Frank Conry
Frank Conry

Reputation: 2716

Knockout Modifying ObservableArray inside Computed

I am trying to solve the problem where I have a two way computed observable and want that parsed into an observable array and have that change event bubble up. The problem is that the it appear to not be notifying subscribers.

Its best shown in jsfiddle: http://jsfiddle.net/RHhmY/13/

Code here:

function CustomerOverview() {
    var self = this;

    self.contacts = ko.observableArray([]);
    self.v = ko.computed(
        {
            read : function(){
            return "";
            },
             write : function(val){
               self.contacts = ko.observableArray(String(val).split(','));
             }
        }
    );
};



var vm = new CustomerOverview();
ko.applyBindings(vm);

and html:

LENGTH<span data-bind="text: contacts.length"></span><br />
<input type="text" data-bind="value: v">

I have tried a number of things including notifying subscribers of the observableArray in question and the length never updates.

As a side not, I'm very open to changing how this is constructed wrt knockout, just want something that works.

Upvotes: 2

Views: 204

Answers (1)

G&#244;T&#244;
G&#244;T&#244;

Reputation: 8053

This breaks your binding:

write : function(val){
           self.contacts = ko.observableArray(String(val).split(','));
}

You want to update your observable instead of re-assigning it

write : function(val){
           self.contacts(String(val).split(','));
}

Upvotes: 4

Related Questions