Skip to content

Instantly share code, notes, and snippets.

@dericcrago
Created February 11, 2012 18:52
Show Gist options
  • Save dericcrago/1803539 to your computer and use it in GitHub Desktop.
Save dericcrago/1803539 to your computer and use it in GitHub Desktop.
Backbone "Layout" View inspired by https://github.com/tbranyen/backbone.layoutmanager
Base.TemplateContext = {};
Base.Views.Layout = Backbone.View.extend({
initialize: function() {
this.setViews(_.extend({}, this.views, this.options.views));
if (this.options.template) {
this.template = this.options.template;
}
this.templateContext = _.extend({}, Base.TemplateContext, this.templateContext);
if (this.options.templateContext) {
_.extend(this.templateContext, this.options.templateContext);
}
},
templateContext: {},
setViews: function(views) {
_.extend(this.views, views);
},
views: {},
render: function() {
this.$el.html(Mustache.render(this.template, this.templateContext));
this.delegateEvents();
_.each(this.views, function(view, key) {
this.$el.find(key).html(view.render().el);
}, this);
return this;
}
});
// example extension
Base.Views.ToolbarView = Base.Views.Layout.extend({
render: function() {
Base.Views.Layout.prototype.render.call(this);
// jquery plugin stuff
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment