javascript - Is there any way to get the name of an Knockout observable object? -


i extending ko.observable write values local storage. i'd use name of variable key, so:

self.newobservable = ko.observable();     self.newobservable.persistvalue(25) 

would persist value in localstorage key of 'newobservable'. there way value 'newobservable'? i've tried using constructor.name returns type name of variable i.e. 'string' or 'number'.

one way give extended observable refence this like

pseudo code (i use .extend() on observable instead)

viewmodel = function() {         this.newobservable = ko.perstableobservable(this); }  ko.perstableobservable = function(owner) {    this.owner = owner;    ... }; 

then can itterate on this , find observable reference

persistvalue = function(value) {    var name = null;    for(var index in this.owner) {       if(this.owner[index] === this) {          name = index;          break;       }    }    ... } 

Comments