【发布时间】:2014-05-28 07:40:29
【问题描述】:
我是淘汰 js 的新手。我想为一个 observable 实现动态验证。为此,我想使用扩展器功能。但它不是在召唤。我创建了jsfiddle。 我的疑问是它什么时候会被调用。
代码是
// Here's my data model
var ViewModel = function(first, last) {
this.firstName = ko.observable(first).extend({logChange: "Sri" });
this.lastName = ko.observable(last);
this.fullName = ko.computed(function() {
// Knockout tracks dependencies automatically. It knows that fullName depends on firstName and lastName, because these get called when evaluating fullName.
return this.firstName() + " " + this.lastName();
}, this);
ko.extenders.logChange = function(target, option) {
alert("log change function")
target.subscribe(function(newValue) {
alert("subscribe function: "+option + ": " + newValue);
});
return target;
};
};
ko.applyBindings(new ViewModel("Hello", "World")); // This makes Knockout get to work
问候, 斯里尼瓦斯
【问题讨论】:
标签: javascript knockout.js knockout-validation knockout-mvc