简体   繁体   English

淘汰赛JS将不会更新映射的对象

[英]Knockout JS will not update mapped object

I'm very new to Knockout JS. 我是Knockout JS的新手。 I haven't found much help through Google on mapping with knockout js. 在Google上使用剔除js映射时,我没有找到太多帮助。 I have a JSON coming in from the server. 我有一个来自服务器的JSON。 I map the JSON with the knockout js mapping library. 我用敲除js映射库映射JSON。 The data-binded dom associated with the property does not update. 与该属性关联的数据绑定的dom不会更新。 I have included a fiddle of the script im using. 我包括了我正在使用的脚本的小提琴。 Notice the json object that gets mapped into the observable. 请注意已映射到可观察对象的json对象。 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. 单击加载按钮时,将映射json,并且应该在标记中显示属性“ test”的值。 Any ideas why its not showing up in the tag? 有什么想法为什么它没有显示在标签中?

There is a working example in the JS fiddle. JS小提琴中有一个有效的示例。

http://jsfiddle.net/sLZmQ/ http://jsfiddle.net/sLZmQ/

This is the JSON that is being mapped 这是正在映射的JSON

 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! 问题是,当您单击loadWebsite时,$ data.test.test没有更新!

I updated and it seems to work with some minor changes: 我更新了,似乎可以进行一些小的更改:

http://jsfiddle.net/bYQu7/ http://jsfiddle.net/bYQu7/

Key point is I dereferenced the 'test' observable: 关键是我取消引用了可观察到的“测试”:

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

And I also initialized the initial value: 我还初始化了初始值:

self.test = ko.observable({ test: ko.observable('WTF') });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM