找到你要的答案

Q:why knockout does not auto update by model changes?

Q:为什么淘汰赛不自动更新模型的变化?

in parse.com I opened up this account g2840904@trbvm.com/12345678. I forked https://github.com/tachang/knockout_tasklist which synchronizes knockout to parse.com and when i start up the app i do see as the only item sometitle1 however if I change sometitle1 to sometitle2 in parse.com task class i don't see this being relfected automatically in the webapp. isn't that what knockout supposed to do when the variable title is defined as observabale?

this.title = ko.observable(data.title);

What should I do so it does reflect automatically changes in model? meaning changing for example sometitle1 to sometitle2 in parse.com would update the web page to reflect that change auotmatically and present sometitle2 instead of sometitle1?

in parse.com I opened up this account g2840904@trbvm.com/12345678. I forked https://github.com/tachang/knockout_tasklist which synchronizes knockout to parse.com and when i start up the app i do see as the only item sometitle1 however if I change sometitle1 to sometitle2 in parse.com task class i don't see this being relfected automatically in the webapp. isn't that what knockout supposed to do when the variable title is defined as observabale?

this.title = ko.observable(data.title);

我应该做什么,所以它反映了模型的自动变化?改变例如sometitle1到sometitle2在parse.com会更新网页反映auotmatically和目前的sometitle2代替sometitle1变化的意义?

answer1: 回答1:

It seems you misunderstand what KO is supposed to be doing. As a client-side library Knockout takes care to synchronize your UI with the underlying data model, which is provided by you via ko.observable variables, and not the database itself. There's a reason why apps are broken into 3 tiers - you've got the front-end which handles UI, the middle tier where all business logic resides and the back-end that serves as data storage. Knockout bridges the middle tier and the front-end, while you are asking for a direct bridge between the front and back-end.

That being said, you can provide this bridge yourself, but of course writing it from a scratch is somewhat painful exercise. The easiest possible way is to ask the db whether any new data has arrived every X seconds using setInterval:

var updateTasks = function () {
    $.parse.get("task", {}, function(json) {
        self.tasks.removeAll();
        for( var i = 0; i < json.results.length ; i++ ) {
             var task = json.results[i];
             self.tasks.push(new Task({ title: task.title, objectId: task.objectId }));
         }
     })
 }
 setInterval(updateTasks, 5000);

看来你误解了KO应该做的事。作为一个客户端库基因敲除照顾同步您的UI与底层的数据模型,这是由你通过ko.observable变量,而不是数据库本身。有一个原因,为什么应用程序被分成3个层次-你有前端处理UI,中间层,所有的业务逻辑驻留和后端作为数据存储。淘汰赛的中间层和前端的桥梁,而你要求的前端和后端之间的直接桥梁。

也就是说,你可以自己提供这座桥,但是从头开始写是有点痛苦的。可能最简单的方法是询问数据库是否有新的数据到达了每X秒使用setInterval:

var updateTasks = function () {
    $.parse.get("task", {}, function(json) {
        self.tasks.removeAll();
        for( var i = 0; i < json.results.length ; i++ ) {
             var task = json.results[i];
             self.tasks.push(new Task({ title: task.title, objectId: task.objectId }));
         }
     })
 }
 setInterval(updateTasks, 5000);
javascript  knockout.js  knockout-2.0