将在何时调用扩展程序函数
When will be the extender function will get called
我是淘汰js的新手。我想实现一个可观察的动态验证。为此,我想使用扩展器函数。但这并不是在呼唤。我已经创建了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
谨致问候,Srinivas
尽管文档中没有明确说明,但
任何自定义扩展程序定义都必须在第一次使用之前提供。
因此,将ko.extenders.logChange
部分移到ViewModel
函数之外:
ko.extenders.logChange = function(target, option) {
alert("log change function")
target.subscribe(function(newValue) {
alert("subscribe function: "+option + ": " + newValue);
});
return target;
};
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);
};
演示JSFiddle。
相关文章:
- 如何从另一个处理程序内部取消JavaScript事件处理程序函数的执行
- 使用jqGrid列格式化程序函数使ng-click工作
- GAS:在处理程序函数中使用变量时,如何更改全局变量值并保留其更改后的值
- AJAX没有使用成功处理程序函数
- extjs中事件处理程序函数中的THIS上下文
- 为什么document.ready中的事件处理程序函数有效但无效;取出后不起作用
- 确定公开JS实用程序函数的最佳模式
- 我在哪里放置我的帮助程序函数
- 在命名处理程序函数中使用 $(this) & event
- 模拟 Web 浏览器方向更改事件以使用 Jasmine 测试绑定事件处理程序函数
- 带参数的移相器按钮处理程序函数
- Angular.js:为什么使用简单的帮助程序函数改变$scope会导致未插值的 {{..}} 表达式
- 循环引用似乎在事件处理程序函数中不起作用
- 在帮助程序函数中使用 for 循环的正确方法 |流星
- 如何通过jQuery $.load()实用程序函数获取PHP将utf-8字符串传递给JavaScript
- JavaScript 事件处理程序函数范围控件
- 何时有时将对象类型定义为将实例绑定方法镜像为构造函数上的静态实用程序函数很有用
- jQuery调用其他事件处理程序中的事件处理程序函数
- 使用 Jasmine 测试 Meteor 帮助程序函数时出错
- 原型方法中的事件处理程序函数,为什么它认为.keyCode是JavaScript中未定义的属性