Observing private properties in Sproutcore

196 Views Asked by At

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

2

There are 2 best solutions below

9
On BEST ANSWER

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):

  • didCreateLayer: the render() method is used to generate new HTML.
    Override this method to perform any additional setup on the DOM you might need to do after creating the view. For example, if you need to listen for events.

Views have changed drastically in SC 1.6 and later, but I believe that didCreateLayer is still recognized.

0
On
(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.