1

I'm very new to Knockout JS. I haven't found much help through Google on mapping with knockout js. I have a JSON coming in from the server. I map the JSON with the knockout js mapping library. The data-binded dom associated with the property does not update. I have included a fiddle of the script im using. Notice the json object that gets mapped into the observable. When the load button is clicked, the json is mapped and the value of the property "test" is supposed to show up in the tag. Any ideas why its not showing up in the tag?

There is a working example in the JS fiddle.

http://jsfiddle.net/sLZmQ/

This is the JSON that is being mapped

 var json = {
"id": 9,
"test": "John Doe",
"firstName": "John",
"lastName": "Doe",
"referenceNumber": "BUY-08",
"position": "Buyer",
"type": "Buyer",
"telephone": "028 82 240780",
"email": "m@email.com",
"departmentId": 3,
"departmentName": "DEPT B",
"country": "United Kingdom"
};

My Example Script

var masterModel = function()
{
var self = this;
self.members = ko.observableArray([]);
self.websites = ko.observableArray([]);    
self.seoData = ko.observable();
self.test =  ko.observable();
self.loadWebsite = function(website)
{

var json = {
"id": 9,
"test": "John Doe",
"firstName": "John",
"lastName": "Doe",
"referenceNumber": "BUY-08",
"position": "Buyer",
"type": "Buyer",
"telephone": "028 82 240780",
"email": "m@email.com",
"departmentId": 3,
"departmentName": "DEPT B",
"country": "United Kingdom"
};

              self.test(ko.mapping.fromJS(json))
              console.log(self.test);


}    
}
    var master = new masterModel();

   ko.applyBindings(master);

The problem is when you click on the loadWebsite the $data.test.test is not updating!

4

1 回答 1

2

我更新了,它似乎可以进行一些小的更改:

http://jsfiddle.net/bYQu7/

关键是我取消了对“测试”可观察的引用:

<h1 data-bind="text: test().test">?</h1>

而且我还初始化了初始值:

self.test = ko.observable({ test: ko.observable('WTF') });
于 2013-04-25T01:32:28.203 回答