javascriptsproutcoresproutcore-views

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


Solution

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