petermichaux / maria

The MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC.
BSD 2-Clause "Simplified" License
764 stars 51 forks source link

Way to extend constructor logic using `subclass` #59

Closed VincentToups closed 11 years ago

VincentToups commented 11 years ago

I prefer my models to never be in an inconsistent state. However, my model cannot be initialized into a consistent state using default values for properties as I might provide to subclass via properties. Is there a method that I can override to run some code when an object is constructed, with the values passed into the constructor, after the superclass constructors are called, which can assert the validity of the construction data and throw an error if the data is invalid?

I'd like to be able to say something like new myNs.myModel(arg1,arg2,...), and have this eventually call a method which I provided to subclass to initialize the object. Is this possible?

VincentToups commented 11 years ago

Well, I see by reading the source code that subclass can take a constructor option which overrides the constructor behavior. I may have missed this in the documentation, but it would be useful to make this feature more prominent.

petermichaux commented 11 years ago

Yes the constructor option is what you want. There is one example showing the use of the constructor option in a view class but it can be used in models and controllers also.

https://github.com/petermichaux/maria/blob/master/eg/checkit/src/js/views/TodosAppView.js#L2

I'll try to see where this can be discussed more in the documentation.