locked
can't access namespace defined in js file in html file

    Question

  • Hi All,

    I have the following .js file and I have defined a namespace in it but I am not able to access that namespace in my HTML file.

    var alarmDataArray = [];
    (function () {
        "use strict";

        var applicationData = Windows.Storage.ApplicationData.current;
        var localFolder = applicationData.localFolder;
        var alarms = [];
        var oneAlarmInfo;

        localFolder.createFolderAsync("AlarmData", Windows.Storage.CreationCollisionOption.openIfExists).then(function (dataFolder) {
            dataFolder.getFileAsync("AlarmsInfo.txt").then(function (dataFile) {
                return Windows.Storage.FileIO.readTextAsync(dataFile).then(function (dataFileText) {
                    dataFileText = dataFileText.substr(1, dataFileText.length);
                    alarms = dataFileText.split(";");
                    for (var i = 0; i < alarms.length; i++) {
                        var ala = alarms[0].split("_");
                        oneAlarmInfo = {
                            name: ala[0],
                            type: ala[1],
                            date: ala[2] + "-" + ala[3] + "-" + ala[4]
                        }
                        window.alarmDataArray.push(oneAlarmInfo);
                    }
                    test();
                }).done();
            },
             function error(e) {
         }).done();
        }).done();


        function test() {
            var dataList = new WinJS.Binding.List(window.alarmDataArray);
            var publicMembers =
            {
                itemList: dataList
            }
            console.log(publicMembers.itemList.getAt(0).date);

            window.WinJS.Namespace.define("DataExample", publicMembers);
        }
    })();

              
    Monday, May 28, 2012 5:51 PM

Answers

  • Don't define the namespace inside of the promise.

    Define this your namespace at the bottom of your anonymous function instead.

    You can load up variables etc... or a function to grab these variables separately.  Of course you will have to be sensitive to the fact that when you call this object or NS function, the promise may not yet be complete!

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, May 29, 2012 1:48 PM
    Moderator

All replies

  • I've run into this before where a system restart actually fixed it. Are you able to hit a breakpoint in script on the html page? If not, then I think it's the same issue and you should just restart. If so, then break on the HTML and use the JavaScript console to see if the DataExample namespace is defined. Good luck.

    (follow me on Twitter at @codefoster)

    Tuesday, May 29, 2012 4:26 AM
  • HI codefoster,

    Thanks for replying.

    I have already done that and namespace is actually not defined. If I don't use a Test function and define the namesapce directly in the main outside function, everything works fine. But I can't do it as createFolderAsync is an asynchronous call and it will try to create the List before dataAlarmArray is populated.

    And the weird part is it executed once successfully but when I executed it again, I got the same bug and haven't seen that mighty metro app since then. :P

    Regards,

    Ravi 

    Tuesday, May 29, 2012 6:24 AM
  • Don't define the namespace inside of the promise.

    Define this your namespace at the bottom of your anonymous function instead.

    You can load up variables etc... or a function to grab these variables separately.  Of course you will have to be sensitive to the fact that when you call this object or NS function, the promise may not yet be complete!

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, May 29, 2012 1:48 PM
    Moderator
  • Thanks Jeff for clarification.

    So is there any way to synchronize the call to NS function with the promise because I want it to execute only after the promise is complete.

    - Ravi

    Tuesday, May 29, 2012 2:43 PM
  • Without knowing what your code is or how you are using this function it is impossible for me to suggest something.  However you can do anything in code that you would do declaritively in HTML so simply move to that model and execute inside of the promise!

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, May 29, 2012 3:16 PM
    Moderator
  • Thanks Jeff, I did it in the code and it worked fine.

    -Ravi

    Tuesday, May 29, 2012 3:52 PM