Search code examples
jsviews

Set an Observer Key in JSViews


Basically can an observer object's key be rename in JSViews?

This is related to stack question and my updated JSFiddle example, in which I rename files where the file name is the object property as well as the key.

My real world example is actually using an onBeforeChange helper to perform the rename which is provided the arguments: oldValue & value. Then I use the oldValue to navigate through the observer object to rename. But, because I don't rename the object key as well further renames will fail because the oldValue is now out of sync.

I hope that explanation plus the above fiddle makes sense... :s

Thanks for you consideration!


Solution

  • You can achieve something close to that by calling:

    $.observable(object).setProperty("newKey", object.key);
    $.observable(object).setProperty("key", undefined);
    

    That will leave a property object.key with the value undefined, but will not actually remove the property.

    If you want you can then call

    delete object.key;
    

    There will probably be a new $(object).removeProperty("someKey") in an upcoming update - which will allow you to write:

    $.observable(object).setProperty("newKey", object.key);
    $.observable(object).removeProperty("key");
    


    UPDATE

    You can now use removeProperty:

    $.observable(object).removeProperty("key");