Computed fields for Backbone.Models
Grab the source from the src folder above. Grab the most recent builds
from the links below.
Development: backbone.compute.js
Production: backbone.compute.min.js
Development: backbone.compute.js
Production: backbone.compute.min.js
varModel=Backbone.Model.extend({initialize: function(){// initialize all of the computed fields for this modelBackbone.Compute(this);},// define a computed field.// tell it what field to `set` on the model// tell it what fields this one depends on// give it a callback function to compute the value when any dependent field changessomeField: {fields: ["f1","f2"],compute: function(fields){returnfields.f1+"-"+fields.f2}}});varmodel=newModel({f1: "foo",f2: "bar"});// get the current valuemodel.get("someField");// => "foo-bar"// re-run the computation, `set` the current value and return itmodel.someField();// => "foo-bar"// Handle "change" events for the computed fieldmodel.on("change:someField",function(){// do stuff when the computed field changes});// `set` the fields that the computed field depends onmodel.set({f1: "boo",f2: "far"});// get the updated valuemodel.get("someField");// => "boo-far"model.someField();// => "boo-far"MIT license - see LICENSE.md