简体   繁体   中英

Observing private properties in Sproutcore

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM