我正在使用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
});
我该怎么做?
答案 0 :(得分:0)
如果您只是想知道是否添加或删除了某些内容,您可以使用外部范围内的变量轻松跟踪该内容。
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;
});
答案 1 :(得分:0)
尝试以下代码,这可能会有所帮助。
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
});