【发布时间】:2013-11-16 09:30:57
【问题描述】:
我有一个简单的字符串集合,我想使用 Knockout 的 foreach 绑定为其创建按钮。该按钮使用单击绑定在我的对象中加载一些数据。绑定正确发生,但实际加载功能失败,告诉我不支持方法。
这是我所拥有的:
//My Object
function MyObject()
{
this.WorkerFunction = function(id)
{
//Do Work
}
this.Load = function(id)
{
//Call Another Function
this.WorkerFunction(id); //Error: Object doesn't support property or method 'WokerFunction'
}
}
//Creation
var myObject = new MyObject();
//VM Binding
var vm = ko.mapping.fromJS(jQueryAjaxObject);
ko.applyBindings(vm);
//HTML
<div data-bind="foreach: stringIDCollection">
<button data-bind="click: function() { myObject.Load($data) }"> Load </button>
</div>
单击按钮时,出现以下错误:Object doesn't support property or method 'WokerFunction'。我的代码中其他地方的鼠标移动事件也出现了类似的错误,但我能够使用下面的绑定函数解决这个问题。但是,我无法让它适用于 Knockout 绑定。
$("myElement").mousemove(this.MouseMove.bind(this));
编辑
我也试过var self = this;和self.WorkerFunction(id);,但我得到了相同的结果;不支持该方法。
问:在保持 this 范围的同时,处理 Knockout Click 绑定到对象函数的正确方法是什么?
【问题讨论】:
标签: javascript jquery knockout.js