【发布时间】:2012-01-25 07:00:21
【问题描述】:
我有类似以下内容:
var ChildViewModel = function (viewModel) {
// state
this.viewModel = viewModel;
this.index = ko.dependentObservable(function () {
return this.viewModel.selections().indexOf(this);
}, this);
this.remove = function () {
this.viewModel.removeSelection(this);
};
this.moveUp = function () {
this.move(-1);
};
this.moveDown = function () {
this.move(1);
};
this.move = function (direction) {
var i = this.index();
this.remove();
this.viewModel.selections.splice(i + direction, 0, this);
};
// additional properties
};
var viewModel = {
selections: ko.observableArray(),
removeSelection: function (item) {
this.selections.remove(item);
},
addSelection: function (event) {
var child = new ChildViewModel(this);
this.selections.push(child );
}
};
ko.applyBindings(viewModel);
当我调用 addSelection 时,我在 KnockoutJS 库中得到了 Object doesn't support this property or method 异常。我的应用程序在 Firefox 3.6 和 Chrome 中运行良好。我在 IE8 中遇到异常。我正在使用 KnockoutJS 的 2.0 版 1.3 Beta。
我做错了什么?
【问题讨论】:
-
您使用的是缩小版的淘汰赛吗?如果是这样,不妨试一试未压缩的版本,看看淘汰赛中的哪一行出错,它可能会为可能的原因提供线索。
-
@AlexKey - 我会更新我的问题。
-
@Daniel:我的错误——我没有意识到 Knockout 有一个
indexOf()的实现。 -
也许 'this' 在 addSelection 中没有适当地绑定 - 你是否从元素绑定中调用 addSelection ? (如果是这样,那个绑定是什么样的?)
标签: javascript mvvm knockout.js