Created
March 3, 2014 05:02
-
-
Save dfernandez79/9318703 to your computer and use it in GitHub Desktop.
Using barman to add traits support to Backbone extend
This file contains hidden or 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
// 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