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

user489998

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'.

Anders

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.

edited at
0

Comments

0 comments
Login to comment

Related

Knockout Subscribe to any change in observable complex object

Can't get Knockout.js to update an observable object property

Is there any way to get the class name of an old style (Borland Pascal) object instance in Delphi 7?

Is there any way to combine following two Observable into a array of object in angular?

Is there a way to get QColor's smart name if any?

Is there any way to get name of the client without prompting it?

There any way to get name of Fortran variable?

Is there any way to get name of request path in Django

Is there any way to get the guild object a command is run it?

is there any way to get the object from its property?

Is there any advantage of keeping a property non observable vs observable in knockout js?

Scala: Proper way to get name of class for an object?

Is there a way to get an object instance name inside a class?

knockout dynamically replace a object in an observable array

Knockout access object values from observable array

Is there a way to get Class from Any (when Any is a instance of a object)?

Knockout observable array get overided when assigning

Cant get data to observable in knockout js

Knockout: Clone observable object into another variable and turn it not observable

'Observable<Object>' is not assignable to type 'Observable<{ user: any; }>'

Is there any way to get offsetheight of an element by its Class Name

xslt any way to get the name of calling template from within a template

Hibernate - Is there any way to get the name of the column for which a Constraint was violated?

is there any way to get city name for Google Place picker android

Is there any way to get job keys in Quartz by job name

Is there any way I can get a package name from terminal?

Is there any way to get logic app name from workflow_Id?

Is there any way to get a WordPress category name from the post title?

Preprocessor macros: any way to get a unique variable name and reuse it?