I have an amount of properties I want to observe with the same shared observer. Let’s say, I want to track modification history. Unfortunately, the standard polymer observer callback, according to the documentation, has an artless prototype:
myObserver: function(oldValue, newValue) {}
Since I want to keep track on changes, I need to know the name of the property changed. By reverse engineering the arguments
of myObserver
, I surprisingly discovered, that there is a third argument of type Arguments[3]
passed to the function. So the code^W woodoo-magic below would do the trick:
myObserver: function() {
if (arguments.length > 2) { // observer called by polymer
var args = arguments[2];
var idx = args[1].length - 1; // sic! index of changed is stored in index
var prop = args[2][idx * 2 + 1][0];
var val = args[0][idx];
console.log("Property [" + prop + "] got new value: " + val);
}
...
}
Being sane and craving to stay sane, I want to know whether I missed more amiable way to get the changed attribute name from the observer.
@ebidel and @robdodson would you please shed some light on that? Would the arguments[2]
be supported in newest versions? Is there a better way to react on changes?
Thanks in advance.