netalab/cojiro — pull request #5 by shioyama (2012-09-05 05:40:54+00:00)
Title: Feature/backbone mixins

I've done a bunch of things in this pull request:

- Create new [base view](https://github.com/netalab/cojiro/blob/3a5666ea74feec7f392f85b92d3de193d68b22fe/app/assets/javascripts/base_classes/base_view.js.coffee) and [base model](https://github.com/netalab/cojiro/blob/3a5666ea74feec7f392f85b92d3de193d68b22fe/app/assets/javascripts/base_classes/base_model.js.coffee) classes to group common patterns, and make all models and views inherit from these classes or their descendants.
- Extract common model methods for translated attributes and group them in a [new translatable fields model](https://github.com/netalab/cojiro/blob/3a5666ea74feec7f392f85b92d3de193d68b22fe/app/assets/javascripts/mixins/translatable_fields_model.js.coffee).
- Extract specs for [translatable fields view](https://github.com/netalab/cojiro/blob/3a5666ea74feec7f392f85b92d3de193d68b22fe/app/assets/javascripts/mixins/translatable_fields_view.js.coffee) from thread model view specs into [separate spec](https://github.com/netalab/cojiro/blob/3a5666ea74feec7f392f85b92d3de193d68b22fe/spec/javascripts/mixins/translatable_fields_view_spec.js.coffee), and isolate from thread model by testing with translatable field model (rather than with thread model).
- Extract specs for source locale into separate [translatable fields model spec](https://github.com/netalab/cojiro/blob/3a5666ea74feec7f392f85b92d3de193d68b22fe/spec/javascripts/mixins/translatable_fields_model_spec.js.coffee).
- Rename "editable fields" --> "translatable fields" to reflect the importance of the "translation" aspect over the "editable" aspect.

One of the results of these changes is that events must be declared a bit differently in views. For details see [this article](http://fiznool.com/post/29347337512/backbone-js-view-inheritance), but basically instead of:

````coffeescript
events:
"click button": "doSomething"
````

you now have to extend the return value of `super`:

````coffeescript
buildEvents: () ->
_(super).extend
"click button": "doSomething"
````

Doing it this way makes it possible to inherit events together with event handlers, which is important for modularity.

The same thing for `initialize` in views:

````coffeescript
initialize: ->
super
````

If you don't call `super` in the initialization method of a view, `delegateEvents` won't be called and so `buildEvents` won't have any effect. At the moment this only matters for views that use events, but in the future we may put other common intialization stuff in the base view initialize method, so really all views should end their initialize method with a call `super` (I haven't updated other views yet to do this).