I am extending ko.observable to write values to local storage. I'd like to use the name of the variable as the key, so:
self.newObservable = ko.observable();
self.newObservable.persistValue(25)
would persist the value in localStorage with a key of 'newObservable'. Is there a way to get the value 'newObservable'? I've tried using constructor.name but that just returns the type name of the variable i.e. 'String' or 'Number'.
One way would be to give the extended observable the refence to this
like
Pseudo code (I would use .extend() on observable instead)
ViewModel = function() {
this.newObservable = ko.perstableObservable(this);
}
ko.perstableObservable = function(owner) {
this.owner = owner;
...
};
Then you can itterate over this
and find the observable reference
persistValue = function(value) {
var name = null;
for(var index in this.owner) {
if(this.owner[index] === this) {
name = index;
break;
}
}
...
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments