我正在使用 knockout_2.0.js。我有一个 observableArray,我在其上设置了订阅。喜欢 :
var Items = ko.observableArray();
Items.subscribe(function(newValue){
//I want to check here whether the item is Added or Deleted from the array
});
我怎样才能做到这一点 ?
我正在使用 knockout_2.0.js。我有一个 observableArray,我在其上设置了订阅。喜欢 :
var Items = ko.observableArray();
Items.subscribe(function(newValue){
//I want to check here whether the item is Added or Deleted from the array
});
我怎样才能做到这一点 ?
如果您只想知道是否添加或删除了某些内容,您可以使用外部作用域上的变量轻松跟踪它。
var items = ko.observableArray();
var itemLen = items.length;
items.subscribe(function (newValue) {
//I want to check here whether the item is Added or Deleted from the array
if (itemLen > items.length) {
// Item removed
} else if (itemLen < items.length) {
// item added
} else {
// something else was modified
}
itemLen = items.length;
});
试试下面的代码,这可能会有所帮助。
var Items = ko.observableArray();
var ItemsLength = ko.observableArray();
this.ItemsLength = ko.computed({
read: function() {
return this.Items().length;
}
});
this.ItemsLength.subscribe(function(newValue){
//you will get control over here whether the
item is Added or Deleted from the array
});