我有一个问题与挖空计算observable和JSON函数。我创建了一个Fiddle Example。在这个例子中,我有一个模型:问题ko.toJSON和计算可观察
function VM()
{
this.Name = ko.observable("Tom");
this.Age = ko.observable(23);
//I want this computed evaluate only
//when name will change. So i put Name observable
//inside it.
ko.computed(function(){
this.Name();
//send only after dom is initiallized
if(initialized){
Server.Sync(this);
}
}, this).extend({ throttle: 500 });
}
function Server()
{
}
Server.Sync = function(data)
{
alert("send");
var jsonData = ko.toJSON(data); //This is the problamatic code which..
//increases the computed dependency. After executing this code the..
//computed function is now evaluates on Age also which i do not want.
//send jsonData
};
在这个模型中,我想我的计算仅评估当用户将更改名称观察特性。其工作正常,直到Server.Sync函数执行完毕。在同步函数我从ViewModel对象创建JSON对象通过toJSON函数和此代码首先打开observables并比创建它的Clean Js对象比通过Stringify它将创建JSON。现在我认为在展开观察数据时,Age可观察到的依赖性会增加我计算的可观察值,现在它会在用户更改Age属性时进行评估。
如果我的解释是正确的,我该如何避免这种情况?
年龄是否有可观察到?看起来你不想观察这些数据的变化。 – 2013-03-08 18:09:27