Is it possible to iterate through mapped observable array and subscribe?

廉价感情. 提交于 2019-12-11 05:07:03

问题


I'm using the knockout mapping plugin to map a collection of objects from the server to an observable array. I'd like to be able to subscribe to some change events on a few properties on those mapped objects. Can anyone point out what I'm doing wrong?

        $.getJSON(apiUrl, function (data) {

            ko.mapping.fromJS(data, {}, self.ReportTemplates);

            for (var i = 0; i < self.ReportTemplates().length; i++) {

                var reportTemplate = self.ReportTemplates()[i];

                //try to subscriber here?
                reportTemplate.VideoId.subscribe = function (a) {
                    alert(a);
                };
            }
        });

回答1:


You're using subscribe wrong. You should be calling it and passing in your handler, i.e.

reportTemplate.VideoId.subscribe(function (a) {
    alert(a);
});


来源:https://stackoverflow.com/questions/14165350/is-it-possible-to-iterate-through-mapped-observable-array-and-subscribe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!