1

I need to assign a javascript object to a ko observable. This object has 75 properties and if I write something like this:

viewmodel.myprop(obj);

it takes very very time and web page crashes.

I thought a work around using a for loop iterating on object properties but I can't write correctly assignment instruction:

for(property in obj){
    // ????
}

How can I do this?

Thank you

pepperav
  • 527
  • 6
  • 16
  • I'm not able to understand the exact problem you have. Are you setting all 75 properties at the same time, causing a certain computed property to be calculated 75 times in that very short timespan? If so, using a loop won't fix the problem; [Change observable but don't notify subscribers in knockout.js](http://stackoverflow.com/q/17983118/247702) might be of use. – user247702 Jul 22 '14 at 11:23
  • I agree there must be some other race condition in effect. Setting an Object as an observable value should not cause a problem like this. – Joel Cochran Jul 22 '14 at 12:24
  • How can I debug knockoutjs assignment? – pepperav Jul 23 '14 at 07:21

1 Answers1

0

In this code properties are dynamically creating

for (property in obj) {
      if (obj.hasOwnProperty(property)) {
          //here you can check all the values on each properties
          console.log(property +" - "+obj[property]);
          //Here new properties are registering for viewmodel
          this[property] = ko.observable(obj[property]); 
      }
 }
 console.log(this.MyProperty());

MyProperty is some matching property name on your large object.

Janith Widarshana
  • 3,213
  • 9
  • 51
  • 73
  • I need to assign obj to viewmodel.myprop() but in a different way of viewmodel.myprop(obj) – pepperav Jul 22 '14 at 11:08
  • just tried but nothing to do. In particular viewmodel prop is valorized (if I check in console) but in page there are no effects. – pepperav Jul 23 '14 at 07:19
  • It works for me in my sample app.In console also I can see the property value.I feel you missed something.I edited the code again.Please check it. – Janith Widarshana Jul 23 '14 at 15:16