Nicholas
Nicholas

Reputation: 391

WinJS binding does not get updated

I want to simply bind the textContent of an html element to a data string. What I do to achieve this is:

<span id="currentDate" data-win-bind="textContent:Data.currentDateString"></span>

The code defines a namespace Data with

WinJS.Namespace.define("Data", {
    currentDateString: currentDateString,
});

where currentDateString is defined as:

var currentDateString = "Monday";

In the ready function of the page I call WinJS.Binding.processAll();. This sets the content of the HTML to the string. But the HTML does not update on a change of the string. I think I have to trigger some event that the string did change. How would I go about that? Is there an easy way as there is the WinJS.Binding.List for list datasources?

Upvotes: 2

Views: 1101

Answers (1)

Dominic Hopton
Dominic Hopton

Reputation: 7292

Your object needs to be observable, which your property-defined-on-a-namespace is not.

Your code needs to change like this:

WinJS.Namespace.define("Data", {
    currentDate: WinJS.Binding.as({
        asString: currentDateString,
    }),
});

Data.currentDate.asString = "Another day!";

When the assignment executes, it will change the value.

There are other helpers for observables, such as WinJS.Binding.define

Upvotes: 4

Related Questions