Backbone, 4- , , , , , , , , .
, , . .
Inside a closure that returns methods, these methods can be divided into two broad categories:
- Specific to the baseline, for example. constructor and initialize
- Custom, specific to your application.
If we return custom methods specific to our application from our separate object, we can use _.bind for the "partial application" _.bindAll only for these custom method names.
Putting it all together:
var foobar = Backbone.Model.extend(function() {
var foobarMethods = modelMethods();
return _.extend({}, foobarMethods, {
constructor: function() {
_.bind(_.bindAll, this, _.keys(foobarMethods));
},
initialize : function() {}
});
function modelMethods() {
return {
foo: function() {},
bar: function() {},
}
}
}());
source
share