Composing with chaplin #232

Closed
mehcode opened this Issue Oct 15, 2012 · 7 comments

Projects

None yet

4 participants

@mehcode
Chaplin – JavaScript Application Architecture Using Backbone.js member

Before I get any further; I need to thank @molily, @paulmillr, @karellm, @9elements, @moviepilot, et al for such an awesome library to build upon.

After our discussion on #202 and after hearing everyone's opinions on the matter I realized that sub-routing was much more of a beast than it had to be. It was much too complicated for the problem it was trying originally to solve.

So from that came composing. Additions are two-fold. The first of which is named regions -- in essence, providing canonical names to selectors. All views can register regions through their regions method. This is not directly related to composing however it complements it greatly as we'll see. The second and last is the ability for views to not only be instantiated and rendered by a controller, but to alternatively be composed. A composed view is instantiated and rendered only if it has not been composed before. Once a view is composed in an action; as long as all subsequent actions likewise compose it, it will not be disposed.

An example shall tell a thousand words: http://concordusapps.github.com/composer-example/
And the source to the example: https://github.com/concordusapps/composer-example

The modifications to chaplin are located at https://github.com/concordusapps/chaplin/tree/topics/composition. Layout is responsible for region management. A new module, Composer is responsible for the composition process and tracking lifetime of composed views.

Still working on the implementation (though its mostly done) and testing before I'll be ready to submit the PR.

Any concerns, ideas, comments?

@cpsubrian

Haven't had a chance to check the code, but the general idea is one we've tackled one-off in a couple of projects and I'm excited for a standardized approach.

@marsch

Wow, that looks exactly what i was looking for.

@cpsubrian

Did a quick glance and I'm not sure about views creating controller instances. Feels backwards.

@paulmillr
Chaplin – JavaScript Application Architecture Using Backbone.js member

Dude, that’s pretty cool! 👍

Could you please open a work-in-progress pull request with changes?

@cpsubrian

btw I'm 100% in support of this feature. Let the bike-shedding commence :)

@marsch

yes am on it too. try to build it directly with-in my new app :D - was looking for a solution for days. and now this! thx.

@mehcode
Chaplin – JavaScript Application Architecture Using Backbone.js member

Few things.

Apologies on leaving the controllers being created by the site view. Meant to change that to be subviews. You're totally right in that in seems a bit backwards.

Basic usage is as follows:

  • Call @initComposer in your class extending from Chaplin.Application's initialize method sometime before @initRouter.
  • To compose a view Inside a controller action, instead of instantiating it, publish `'!composer:compose' and pass it the view class and arguments for its constructor like:
@publishEvent '!composer:compose`, ViewClass

Let me clean a few things up and I'll submit that PR shortly then. Thanks for the support.

@paulmillr paulmillr closed this Oct 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment