I find it amazing how Google Closure library provides not only functionality, but a methodology and a guide to create a solid architecture of components.
goog.ui.Control
inherits from goog.ui.Component
which defines the getModel
and setModel
. This makes me think it's intended for MVC:
goog.ui.Control
goog.ui.ControlRenderer
goog.ui.Control.model_
propertyI couldn't find a single example of usage of this model in the whole library. I believe it's kept for complexer modules Google uses for itself. Nevertheless, the idea doesn't stop from being interesting.
Now, the question is oriented to this: where and how should you initialize the model of a goog.ui.Control
: inside of the contructor? Should it be given as a parameter to the control? Should it be set from the outside on an initialization?
Making my question I realized the answer (or at least what I think is a good approach). I'm leaving this anyway here for the future me and any other person which is interested:
A complex model probably depends on additional initialization parameters. It doesn't matter how the component initializes, if it wants to know about it's context it must receive help from the outside.
In a way, this is quite similar to the requirements of initialization for rendering. Both, render
and decorate
need an additional parameter to work, which is where they will be working, under which element.
The pattern here is similar to builder: after you have the instance, you start providing additional initialization data. This could be easily done with the model. And about order between view and model, model is probably more important, maybe vital, to the initialization of the view. Which leads to the result:
Builder design pattern, with initialization data call previous to the rendering.