2

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.

Frank Conry
  • 2,694
  • 3
  • 29
  • 35

1 Answers1

4

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(','));
}
GôTô
  • 7,974
  • 3
  • 32
  • 43
  • 2
    this was a huge essential point I was missing, really getting me over the hump with knockout, thanks so much! – Frank Conry Jul 12 '14 at 07:06