none
What use is WinJS.Namespace for metro app?

    Question

  • I wonder what function WinJS.Namespace in a WinJS app has?

    Couldn't the same be achieved by standard javascript object class like 

    ConferenceApp {
      Api: {
       getSessions: function() {}
      }
    }
    


    This would create ConferenceApp.Api object inside the window object?

    The docs are not clear for what we need to register our methods that way.

    Thursday, November 17, 2011 12:54 AM

All replies

  • Hi Phil,

    The WinJS name space not only scopes different things but contains various helper functions.  It is implemented in the files in the WinJS directory when you create a new application.  Feel free to poke around those JS files and see how it is implemented and the many things you can use from the WinJS namespace.

    There is a ton of information about the WinJS namespace!  Start here for a good overview: http://msdn.microsoft.com/en-us/library/windows/apps/br211669.aspx

    I am not sure what you are asking with this question, can you be more specific? :"The docs are not clear for what we need to register our methods that way"

     

    -Jeff


    Jeff Sanders (MSFT)
    Thursday, November 17, 2011 1:24 PM
  • Thanks Jeff for your constant feedback!
    The thing the WinJS namespace offers can also be done in plain java. Instead of WinJS.define("MyNamespace.api", ...) I could just use plain old js like this:
    MyNamespace = {
      api = {
        method1: function() {
        }
      }
    }

    Of course, if someone else would like to extend my namespace later, it would be difficult do do in plain js.
    So that is one benefit of WinJS, since I saw an "extend" or something like that method in it. "defineWithParent" I think it was called.
    What other benefits it offers I have yet to find out :)
    Sorry for the many questions :) Coming from Ruby I have again to settle in with javascript and how it is used in WinJS. But overall impression is very good (although I will use jquery and not getElementById and $(e).live() for convenience). I just hope that IE has less memory leaks when it comes to js in Windows 8 :)
    Otherwise I would have to port the prototype back to C++.
    Thursday, November 17, 2011 2:34 PM
  • Hi Phil,

    My pleasure!  I am sure your questions will help other community members and you will be able to help others too in the forums!

    Yes the WinJS namespace in fact is just a javascript library.  You could do everything yourself without utilizing it.  It is good that you are drilling into the js files that are provided.  It will help you learn some of the advanced javascript language constructs and how we are utilizing then to simplify some common tasks.  The cool think is that you can use other libraries like jQuery as well if you want!

    -Jeff

     


    Jeff Sanders (MSFT)
    Thursday, November 17, 2011 2:41 PM
  • What about the memory leaks that plagued the old IEs? I often see addListener in metro apps but no removeListener? Is that nothing to be worried about?
    Thursday, November 17, 2011 2:52 PM
  • Thanks Jeff for your constant feedback!
    The thing the WinJS namespace offers can also be done in plain java. Instead of WinJS.define("MyNamespace.api", ...) I could just use plain old js like this:
    MyNamespace = {
      api = {
        method1: function() {
        }
      }
    }

    Of course, if someone else would like to extend my namespace later, it would be difficult do do in plain js.
    So that is one benefit of WinJS, since I saw an "extend" or something like that method in it. "defineWithParent" I think it was called.
    What other benefits it offers I have yet to find out :)
    Sorry for the many questions :) Coming from Ruby I have again to settle in with javascript and how it is used in WinJS. But overall impression is very good (although I will use jquery and not getElementById and $(e).live() for convenience). I just hope that IE has less memory leaks when it comes to js in Windows 8 :)
    Otherwise I would have to port the prototype back to C++.

    My understanding is that the WinJS namespace declarations help with Intellisense, and that using them helps your code be consistent with system-provided code. If you prefer not to do it that way there are definitely alternatives, but the one you show has problems - it prevents encapsulation because you can't use closures to hide things that the functions share and shouldn't be shown to the outside world. Here's a link describing other patterns for namespace declaration: http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/. Hope this helps!
    Thursday, November 17, 2011 8:56 PM
  • Thank you Ted, I will encourage the use of WinJS namespace! Intellisense is a good thing
    Friday, November 18, 2011 6:01 AM