问题描述:

I'm amending some knockout code and I've encountered this :

this.edit = ko.observable();

this.selected = ko.observable();

this.clicked = ko.observable();

Assigning multiple references to the same ko.observable does not make sense to me. Is there a reason it is done in this way ?

I think it should just be something like :

this.genericName = ko.observable();

And then just use this.genericName wherever this.edit,this.selected or this.clicked are used.

网友答案:

A ko.observable(); returns a single new observable property, unlike an object with as set of observable properties like model objects in Backbone. To have an object with multiple observable properties, you need to create multiple observables using the ko.observable() factory method.

You can see that they are independent:

var a = ko.observable();
var b = ko.observable();
a(1);
b(2);
console.log(a()); // prints 1
相关阅读:
Top