3

I have an observableArray of objects in my view model, and within, I have an observable property on one key:

var somefunc = function(sysMsgs)
{
    // Create a VO from passed array
    $.each(sysMsgs, function(i, v) {
    var tMsg = {
        ...
        READ : ko.observable(v.READ),
        ...
    };
    systemMessagesArray.push(tMsg); // Already Initialized observableArray
   });
}

I need to subscribe to changes on the READ key so that I can disable form controls in the view. On a regular observable, I would:

variable.subscribe(function(v) {do something with the value v;});

Any idea on how to scribe to the key within the array? Thanks...

1 Answers1

0

Have you tried that ?

var somefunc = function (sysMsgs) {
    // Create a VO from passed array
    $.each(sysMsgs, function (i, v) {
        var tMsg = {
            READ: ko.observable(v.READ),
        };
        systemMessagesArray.push(tMsg);
        tMsg.subscribe(function {
            alert('changed');
        });
    });
};
Damien
  • 8,889
  • 3
  • 32
  • 40
  • Thanks for your response! The tMsg is a local var and has not been added to KO as an observable. I did try all of these variations: systemMessagesArray().subscribe(function(v) {alert('changed');}); And: systemMessagesArray().READ.subscribe(function(v) {alert('changed');}); I've also tried with out calling the array as a method (without the parenthesis) and nothing. – user2250351 May 01 '13 at 18:56
  • Could you make a fiddle to illustrate the problem ? – Damien May 01 '13 at 19:11
  • I wonder how memory intensive this is. It would be better if there was a way to say "sysMsgs.subscribe("tMsg", function(){ console.log("changed"); })" – netpoetica Aug 29 '14 at 14:42