简体   繁体   English

修改可观察数组时如何触发函数

[英]how to fire function when observable array is modified

is it possible to fire a function when observable array is modified? 修改可观察数组时,是否可以触发函数? my goal here is to notify me if my observable array is modified to do some logics on my current application 我的目标是通知我是否将可观察数组修改为对当前应用程序执行一些逻辑

this is my view model it has a observable array inside it 这是我的视图模型,里面有一个可观察的数组

WT.BM.BarsViewModel = function () {
    var self = this;
    self.BarsDataHolder = ko.observableArray([]);
};

i just want to fire a function to notify me if self.BarsDataHolder has been modified 我只想触发一个函数来通知我,如果self.BarsDataHolder已被修改

any ideas? 有任何想法吗?

You can subscribe to any ko.observable or ko.observable array. 您可以订阅任何ko.observable或ko.observable数组。

WT.BM.BarsViewModel.BarsDataHolder.subscribe(function(newArray) {
   console.dir(newArray);
});

you can find this in the knockout documentation on the observables page 您可以在可观察页面的淘汰赛文档中找到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM