Skip to content

Instantly share code, notes, and snippets.

@dfernandez79
Created March 3, 2014 05:02
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 dfernandez79/9318703 to your computer and use it in GitHub Desktop.
Save dfernandez79/9318703 to your computer and use it in GitHub Desktop.
Using barman to add traits support to Backbone extend
// 1. Overwrite your Backbone extend with barman.Nil.extend
Backbone.Model.extend =
Backbone.Collection.extend =
Backbone.Router.extend =
Backbone.View.extend =
Backbone.History.extend = barman.Nil.extend;
// 2. Now you can use traits (a special case of mixin) when using extend:
// a silly example
var templateViewTrait = {
template: barman.required,
render: function () {
this.$el.html(_.result(this, 'template'));
return this;
}
};
// another trait
var handlebarsSupport = {
hbsTemplate: barman.required,
template: function () {
var fn = Handlebars.compile(this.hbsTemplate);
return fn({model: this.model.toJSON()});
}
};
// note the additional (and optional) paramater to extend
// see https://github.com/dfernandez79/barman for more details
// barman extend is compatible with Backbone extend
var contact = new Backbone.Model({name: 'Barman'});
var MyView = Backbone.View.extend([templateViewTrait, handlebarsSupport], {
hbsTemplate: '<h1>Hello {{model.name}}!!</h1>'
});
$('#container').html(new MyView({model: contact}).render().el);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment