locked
Unable to Nest functions in WinJs

    Question

  • HI,

    I am attempting to Nest functions in WinJS. The following logic is easy to implement in JavaScript but I am getting an "JavaScript runtime error: 'Foo2' is undefined' when I implement it using WinJS.

    Code is in Home.js
    =============================================================
    (function() {
                            ----
                     
                            WinJS.UI.Pages.define("/pages/home/home.html", {

                                       ready: function(element, options){

                                            -----------

                                            var fooButton = element.querySelector("#fooButton");
                                            fooButton.addEventListener("click", this.Foo1, false);

                                            -----------

                                      },


                                        Foo1: function()
                                        {
                                             ----
                                             ----
                                             setTimeOut(‘Foo2()’,3000);

                                        },

                                        Foo2: function()
                                       {
                                            ----
                                            ----
                                            setTimeOut(‘Foo3()’, 3000);
                                        },

                                        Foo3: function()
                                       {
                                            ----
                                        }
                       });
    })();
    ===============================================================

     Hoping someone knows how to implement this in WinJS.

    Thanks!

     

    Tuesday, February 12, 2013 7:52 PM

Answers

All replies

  • The problem is that you need to prefix your calls with the "this" keyword in your current setup. If you don't want to have to deal with that, and I don't like to, I would recommend you change the way you're defining your object (object literal the way you have it) to the revealing module pattern. See the second half of the accepted answer here for more info: http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/thread/5e2c03a4-16ca-47eb-8a42-c950b3ffdf83/#572815c0-2ee5-4be7-9c8f-741159aae1fc
    • Marked as answer by Jay_Hawk Tuesday, February 12, 2013 11:07 PM
    Tuesday, February 12, 2013 7:57 PM
  • Thanks a lot!!

    Final solution for the benefit of others:

    =============================================================
     (function() {
                             ----
                     
                             WinJS.UI.Pages.define("/pages/home/home.html", {

                                       ready: function(element, options){

                                            -----------

                                            var fooButton = element.querySelector("#fooButton");
                                             fooButton.addEventListener("click", this.Foo1.bind(this), false);

                                            -----------

                                      },


                                         Foo1: function()
                                         {
                                              ----
                                             ----
                                             setTimeOut(this.Foo2.bind(this),3000);

                                        },

                                        Foo2: function()
                                        {
                                             ----
                                            ----
                                            setTimeOut(this.Foo3, 3000);
                                         },

                                        Foo3: function()
                                        {
                                             ----
                                        }
                        });
     })();
     ===============================================================

    Tuesday, February 12, 2013 11:07 PM