Observable通知父ObservableArray [英] Observable notify parent ObservableArray

查看:76
本文介绍了Observable通知父ObservableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个observableArray fieldChoices,其中包含类似于以下内容的可观察对象:{ name: ko.observable('new choice') }

I have a observableArray fieldChoices that contains observables like the following ones: { name: ko.observable('new choice') }

我想让Observables通知ObservableArray,后者必须通知另一个已计算的对象.

I want to get the Observables to notify the ObservableArray which has to notify another computed.

我这样绑定他们:

 <div data-bind="foreach: $parent.fieldChoices">
     <div>
        <i class="icon-caret-right"></i>
        <input type="text" class="span8" data-bind="value: name(), test: name()" />
        <i class="icon-arrow-up" data-bind="click: $parentContext.$parent.shiftChoiceUp, visible: $index() !== 0"></i>
        <i class="icon-arrow-down" data-bind="click: $parentContext.$parent.shiftChoiceDown, visible: $index() !== ($parentContext.$parent.fieldChoices().length - 1)"></i>
        <i class="icon-remove" data-bind="click: $parentContext.$parent.removeChoice, visible: $parentContext.$parent.fieldChoices().length > 2"></i>
     </div>
</div>

name值更改时,我无法使可观察对象通知可观察数组.我试图添加一个自定义绑定test: name()并从那里进行通知.问题在于,更改值后绑定不会更新.

I cannot get the observables to notify the observable array when the name value changes. I tried to add a custom binding test: name() and notify from there. The problem is that the binding is not updated when the value is changed.

我该如何工作?

即使存在类似的问题,我仍试图通过自定义绑定来解决.我找不到这种方法的任何解决方案.

EDIT : Even though there are similar questions I am trying to solve this with a custom binding. I wasn't able to find any solutions for this approach.

推荐答案

这个问题对我来说并没有多大意义,因为默认情况下,您所描述的内容有效(当值更改时,可观察对象会通知其订阅者).您可以为可观察对象创建扩展程序,以便在需要时显式通知您的observableArray,但您可能不希望通知observableArray该可观察对象已更改(因为它不会通知其他任何人,因为它是未更改),您可能只想执行一些常用功能-

This question doesn't make a ton of sense to me because by default what you are describing works (observables notify their subscribers when values change). You can create an extender for your observables which notify your observableArray explicitly if you want, but you probably don't want to notify the observableArray that the observable changed (because then it won't notify anyone else, because it's value hasn't changed) you probably just want to do some common function -

创建一个可观察的数组-

Create an observable array -

var myArray = ko.observableArray();

注册扩展程序-

ko.extenders.trackChildChanges = function (target, value) {
    target.subscribe(function () {
        // Do some notification
    });
    return target;
};

然后创建您的可观测对象并对其进行扩展-

And then create your observables and extend them -

function child(object) {
    var self = this;
    self.Id = ko.observable(id).extend({trackChildChanges: true});
    self.Name = ko.observable(name)extend({trackChildChanges: true});
}

var regArray = [{ id: 1, name: 'Bill'}, {id: 2, name: 'John'}];

$.each(regArray, function(index, item) {
    myArray.push(new child(item));
});

这篇关于Observable通知父ObservableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆