locked
How to make a variable in the WinJS.Namespace observable?

    Question

  • I want to have some member of the global namespace "app" observable.

    So I declare:

    WinJS.Namespace.define("app", WinJS.Binding.as({
      bill: null
    });

    That gives me "app.bill === null". Now if I call "app.bill = new Bill()" it does not trigger any bound listeners.

    Friday, June 20, 2014 12:23 AM

All replies

  • Hi pkursawe,

    The second parameter of the define function declares:

    members

    Type: object

    The members of the new namespace.

     

    Here you use a Binding, its definitely null without binding. Or you could do something like this:

    WinJS.Namespace.define("app", {
        bill: WinJS.Binding.as({
            billCollection: someBillCollection,
        }),
    });
    
    bill.billCollection = xxx;
    --James

    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Marked as answer by Jamles HezModerator Thursday, July 3, 2014 6:24 AM
    • Unmarked as answer by pkursawe Wednesday, July 9, 2014 1:45 AM
    Monday, June 23, 2014 9:39 AM
    Moderator
  • James, what you offer as solution is not the scenario I need. The namespaces app.bill needs to be observable by itself. It could be a string or an object, doesn't really matter. My use-case requires that when "app.bill" (the var, not the content!) changes, listeners of that object "app.bill" are triggered.

    So "app.bill.amount = 1" is NOT my scenario. That works already. But I have UI elements that should not be shown when "app.bill" is null. So the UI elements bind to this property exactly like this:

    <span data-win-bind="style.display:app.bill binding.displayIf">

    <span data-win-bind="textContent:app.bill.amount"></span>

    </span>


    My use-case is "app.bill = new Bill()" or "app.bill = null"

    Wednesday, July 9, 2014 1:52 AM