Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Computed fields for Backbone.Model
branch: master
Failed to load latest commit information.
lib
public/javascripts
spec/javascripts
src build v0.0.1
.gitignore initial commit with basic structure and some not yet implemented tests
CHANGELOG.md changelog for v0.0.1
LICENSE.md
grunt.js
package.json version bump to v0.0.1
readme.md remove unused line in example

readme.md

Backbone.Compute

Computed fields for Backbone.Models

Downloads And Source

Grab the source from the src folder above. Grab the most recent builds from the links below.

Standard Builds

RequireJS (AMD) Builds

Basic Use

var Model = Backbone.Model.extend({

  initialize: function(){
    // initialize all of the computed fields for this model
    Backbone.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 changes
  someField: {
    fields: ["f1", "f2"], 
    compute: function(fields){
      return fields.f1 + "-" + fields.f2
    }
  }

});

var model = new Model({
  f1: "foo",
  f2: "bar"
});

// get the current value
model.get("someField"); // => "foo-bar"

// re-run the computation, `set` the current value and return it
model.someField(); // => "foo-bar"


// Handle "change" events for the computed field
model.on("change:someField", function(){
  // do stuff when the computed field changes
});

// `set` the fields that the computed field depends on
model.set({
  f1: "boo",
  f2: "far"
});

// get the updated value
model.get("someField"); // => "boo-far"
model.someField(); // => "boo-far"

License

MIT license - see LICENSE.md

Something went wrong with that request. Please try again.