Is it possible to observe private (underscored ) properties from within the object itself? I need to know when _view_layer is getting set, so that I can apply some jQuery even handlers. Unfortunately init() and render() are really early, so _view_layer is undefined. Unfortunately, observing _view_layer doesn't seem to work as well. Please, tell me what I can do. Basically, if there is another possible solution, I am open to see that as well
Observing private properties in Sproutcore
199 Views Asked by user802232 At
2
There are 2 best solutions below
0

(function() {
var value = obj._view_layer;
delete obj._view_layer;
var callback = function() {
/* observation logic */
}
Object.defineProperty(obj, "_view_layer", {
get: function() {
return value;
},
set: function(val) {
value = val;
callback(val);
},
writable: true,
enumerable: true
});
})();
Requires an ES5 browser.
Only recommended to use for debugging. You can also use .watch
when debugging in firefox.
In Sproutcore the underscore is only a convention that the property/method is private. Its not actually private.
In Sproutcore, the views have life-cycle methods. This one might be of interest (taken from SC 1.4.5 code in view):
Views have changed drastically in SC 1.6 and later, but I believe that
didCreateLayer
is still recognized.