-
-
Save zoghal/2847542 to your computer and use it in GitHub Desktop.
template containers in Ember
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{#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