Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoghal/2847542 to your computer and use it in GitHub Desktop.
Save zoghal/2847542 to your computer and use it in GitHub Desktop.
template containers in Ember
// A mixin for `Em.ContainerView` that allows an initial render from
// a template, followed by further additions by mutating childViews.
// Note that the template should *only* contain calls to
// `{{childView}}`
App.Mixins.TemplateContainerView = Em.Mixin.create({
render: function() {
if (!this.get('_renderedFromTemplate')) {
Em.View.prototype.render.apply(this, arguments);
this.set('_renderedFromTemplate', true);
} else {
return this._super.apply(this, arguments);
}
}
});
App.Views.TemplateContainerView = Em.ContainerView.extend(App.Mixins.TemplateContainerView);
// Essentially function(name = "Em.View", options = {data: {}, hash: {}})
var parseHelperArguments = function(viewClassNameOrOptions, options) {
var viewClass;
if (_.isString(viewClassNameOrOptions)) {
viewClass = Em.getPath(viewClassNameOrOptions);
window.ember_assert( "Could not find view class %@".fmt(viewClassNameOrOptions), viewClass != null );
options = options || {};
} else {
viewClass = Em.View;
options = viewClassNameOrOptions || {};
}
options.data = options.data || {};
options.hash = options.hash || {};
return [viewClass, options];
};
var addChildView = function() {
var parsedArguments = parseHelperArguments.apply(this, arguments),
viewClass = parsedArguments[0],
options = parsedArguments[1],
parentView = options.data.view,
childViewClass,
childView;
window.ember_assert( "childView can only be used inside of an App.Mixins.TemplateContainerView", App.Mixins.TemplateContainerView.detect(parentView) );
childViewClass = Em.Handlebars.ViewHelper.viewClassFromHTMLOptions(viewClass, options.hash, this);
childView = parentView.createChildView(childViewClass, {
template: options.fn
});
parentView.get('childViews').pushObject(childView);
};
Handlebars.registerHelper('childView', addChildView);
{{#view App.TemplateContainerView id="main_panes"}}
{{#childView id="dashboard"}}
...
{{/childView}}
{{#childView id="inbox"}}
...
{{/childView}}
{{/view}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment