An small and simple implementation of computed properties for Backbone.js
Inspired by computed properties in Ember.js and dependency injection Angular.js.
Prior Art:
- Backbone mailing list thread: Backbone.Model Computed Attributes
- Blog post: Backbone.js computed properties
- GitHub: kdocki / backbone.model.computed
- GitHub: phdesign / Backbone.Spark
- GitHub Gist: tjbladez / computed.coffee
By Curran Kelleher 3/23/2014
Here's the implementation:
function addComputedProperty(model, computedProperty, dependencies, fn){
var callFn = _.debounce(function(){
var args = dependencies.map(function (property){
return model.get(property);
});
model.set(computedProperty, fn.apply(null, args));
});
dependencies.forEach(function(property){
model.on('change:' + property, callFn);
});
}
// Example usage:
var model = new Backbone.Model({
firstName: "Joe",
lastName: "Schmoe"
});
addComputedProperty(model, 'fullName',
['firstName', 'lastName'],
function (firstName, lastName){
return firstName + ' ' + lastName;
});
See addComputedProperty.js
for a documented implementation, and example.js
for a documented and more complete example.