locked
x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'unitTemp'

    Question

  • I get following error when I try to call a method through another method. Both methods are in the same object.

    x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'unitTemp'

    my code is like this :: (excluding long details)

    var obj = {

    unitTime : function() {

    },

    unitTemp : function() {

            this.unitTime();                //This throws above error.

    }

    };

     

    Monday, March 11, 2013 7:02 PM

Answers

  • No repro, here is what I tried:

    (function () {
    ....
        app.onactivated = function (args) {
                args.setPromise(WinJS.UI.processAll().done(function () {
                    obj.unitTemp();

                }));
    ....
        app.start();

        var obj = {
            unitTime: function () {
                console.log("unitTime called");
            },
            unitTemp: function () {
                this.unitTime();                //This throws above error.
            }
        };

    I get the "unitTime called" message in the JavaScript console window.


    Monday, March 11, 2013 7:23 PM
    Moderator

All replies

  • No repro, here is what I tried:

    (function () {
    ....
        app.onactivated = function (args) {
                args.setPromise(WinJS.UI.processAll().done(function () {
                    obj.unitTemp();

                }));
    ....
        app.start();

        var obj = {
            unitTime: function () {
                console.log("unitTime called");
            },
            unitTemp: function () {
                this.unitTime();                //This throws above error.
            }
        };

    I get the "unitTime called" message in the JavaScript console window.


    Monday, March 11, 2013 7:23 PM
    Moderator
  •           Thanks for looking at this.

              I'm calling my function in navigator template, home.js

         

    (

       WinJS.UI.Pages.define(

    "/pages/home/home.html", {

      

            ready: function(element, options) {

                         this.display();      

                   },

                 

    unitTime : function() {

    },

    unitTemp : function() {

            this.unitTime();                //This throws above error.

    },

    display: function() {

    }

    };

    So, how do I use it here?

     

                 

                 As far as I know this function in navigator app template automatically calls WinJS.UI.ProcessAll function and I can register my events inside ready function. I have defined all other methods below ready function.



    • Edited by SonalMac Monday, March 11, 2013 8:08 PM
    Monday, March 11, 2013 7:36 PM
  • Still no repro. If you have a repro app, you can share it through SkyDrive and I can take a look at it. I created a "Navigation App" template and then modified home.js with the following contents and I cannot repro.

    (function () {
        "use strict";

        WinJS.UI.Pages.define("/pages/home/home.html", {
            // This function is called whenever a user navigates to this page. It
            // populates the page elements with the app's data.
            ready: function (element, options) {
                // TODO: Initialize the page here.
                this.unitTemp();
            },
            unitTime: function () {
                console.log("unitTime called")
            },
            unitTemp : function() {
                this.unitTime();                //This throws above error.
            }

        });
    })();

    Monday, March 11, 2013 8:32 PM
    Moderator
  •         thanks again. I have uploaded my project files in my SkyDrive. I would like to solve other problems also, like storing and retrieving app/user data in this project. How do I share it with you? (This is the first time I'm using SkyDrive..)

    My skydrive shows my name like this "Sonal's SkyDrive". So may be "sonal" is my username on it.


    • Edited by SonalMac Monday, March 11, 2013 9:25 PM
    Monday, March 11, 2013 8:44 PM
  • http://windows.microsoft.com/en-US/skydrive/sharing-files-photos.

    For other unrelated questions, please post another question on the forum so that it will benefit the community.

    Monday, March 11, 2013 10:07 PM
    Moderator
  •   I have uploaded my Visual Studio project on Public folder of my skydrive. And link for that is:        

    https://skydrive.live.com/redir?resid=F4CAEFCD620982EB!105&authkey=!AE-ziM-BLJuYj7A

    Thanks for all your help. Though the link for sharing-files-photos is not working.

     
    Tuesday, March 12, 2013 5:15 AM
  • The zip file is incomplete, it does not include the JavaScript files and other dependencies.
    Tuesday, March 12, 2013 4:38 PM
    Moderator
  • ok, Sorry about it. This time I have zipped the folder and uploaded it to SkyDrive. The link now is:

    https://skydrive.live.com/redir?resid=F4CAEFCD620982EB!105&authkey=!AE-ziM-BLJuYj7A

    Tuesday, March 12, 2013 6:06 PM
  • Hi Sonal,

    When adding the event listener, the scope of the "this" object is the actual control object, however when you actually perform the event such as "change" or "click", the scope of the "this" object changes to the actual window object - such as button or textbox. To make sure that the event handler is referring to the control object rather than the window object, you need to "bind" the control object when adding the event listener in the definition.

    In short, do this:

                cat.addEventListener('change', this.updateMenu.bind(this), false);
                fromUnit.addEventListener('change', this.convertVal.bind(this), false);
                toUnit.addEventListener('change', this.convertVal.bind(this), false);
                fromLength.addEventListener('change', this.convertVal.bind(this), false);

                document.getElementById("saveBtn").addEventListener('click', this.saveBtnClicked.bind(this), false);

    Notice the .bind(this) after the function name. This should ensure that you get the correct behavior. See this link for more info: http://msdn.microsoft.com/en-us/library/windows/apps/Hh770579.aspx (the sample code has a comment about the bind method)

    Thanks,

    Prashant.

    Tuesday, March 12, 2013 11:26 PM
    Moderator
  • saveBtnClicked: function(eventInfo) {
    
        var saveButton = document.getElementById("saveBtn");
        var savedOutput1 = document.getElementById("savedOutput1");
                 
        counter = counter + 1;
    
        roamingFolder.createFileAsync(filename, Windows.Storage.CreationCollisionOption.replaceExisting)
            .then(function (filename) {
                var dataToSave = {
                    "result": '"' + toLength.innerText + '"',
                    "count":  counter 
                };
    
                console.log(dataToSave.result);
                console.log(dataToSave.count);
    
                var savedData = JSON.stringify(dataToSave);
                return Windows.Storage.FileIO.writeTextAsync(filename, savedData);
            }).done(function () {
                this.filesDisplayOutput();
              });

      Hello Prashant,

                Thanks a lot. This shades light on an important concept. It took me many days of searching online but I come to know this by your answer only.

    However, now my code shows same error for following part. I understand, this call to "this.filesdisplayoutput();" has to do with "context." But I don't know how to call this function here. Please guide me here as well.

    Wednesday, March 13, 2013 6:00 AM
  • Hi Sonal,

    The problem happens because you need to chain the "this" object by saving a local copy and then using it in the completion of the asynchronous function. Basically, all you have to do is the below. If you don't save the local copy, the "this" object is lost when the asynchronous function completes.

    saveBtnClicked: function() {
        ....
        var that = this;
        ....
        roamingFolder.createFileAsync(filename, Windows.Storage.CreationCollisionOption.replaceExisting)
            .then(function (filename) {
                ....
                ....
            }).done(function () {
                that.filesDisplayOutput();
            });

    Thx,

    Prashant.

    Wednesday, March 13, 2013 5:35 PM
    Moderator
  •  Yes, I got it. Thanks a lot once again.  Now I'm having problem of saving data in the file and retrieving them. I'm opening a new question for that. Please give a look at that if you got time.

    Regards

    Sonal

    Wednesday, March 13, 2013 6:30 PM