Skip to content

Instantly share code, notes, and snippets.

@sontek
Created September 11, 2012 12:17
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 sontek/3697998 to your computer and use it in GitHub Desktop.
Save sontek/3697998 to your computer and use it in GitHub Desktop.
Example of how to manage embedded views with Backbone.js
Layout = Backbone.View.extend({
initialize: function() {
var me = this;
this.setViews(_.extend({}, this.views, this.options.views));
if (this.options.template) {
this.template = this.options.template;
}
if (this.options.menu) {
this.menu = this.options.menu;
}
this.templateContext = _.extend({},
this.templateContext,
this.options.templateContext
);
}
, setViews: function(views) {
this.views = _.extend({}, this.views, views);
}
, views: {}
, render: function() {
console.log(this.templateContext);
var t = Mustache.to_html($(this.template).html(), this.templateContext);
this.$el.html(t);
_.each(this.views, function(view, key) {
this.$el.find(key).html(view.render().el);
}, this);
return this;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment