Skip to content

Commit

Permalink
alternate syntax for computed field definition using object literal
Browse files Browse the repository at this point in the history
  • Loading branch information
Derick Bailey committed Sep 24, 2012
1 parent 021866a commit 660508d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
21 changes: 12 additions & 9 deletions readme.md
Expand Up @@ -33,10 +33,13 @@ var Model = Backbone.Model.extend({
// 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
computedField: Backbone.Compute("computedField", ["f1", "f2"], function(fields){
fieldList = fields;
return fields.f1 + "-" + fields.f2
})
someField: {
fields: ["f1", "f2"],
compute: function(fields){
fieldList = fields;
return fields.f1 + "-" + fields.f2
}
}

});

Expand All @@ -46,14 +49,14 @@ var model = new Model({
});

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

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


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

Expand All @@ -64,8 +67,8 @@ model.set({
});

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

## License
Expand Down
49 changes: 32 additions & 17 deletions spec/javascripts/computedField.spec.js
Expand Up @@ -8,10 +8,13 @@ describe("computed fields", function(){
Backbone.Compute(this);
},

computedField: Backbone.Compute("computedField", "anotherField", function(fields){
fieldList = fields;
return fields.anotherField + "-bar"
})
computedField: {
fields: "anotherField",
compute: function(fields){
fieldList = fields;
return fields.anotherField + "-bar"
}
}
});

beforeEach(function(){
Expand All @@ -37,10 +40,13 @@ describe("computed fields", function(){
Backbone.Compute(this);
},

computedField: Backbone.Compute("computedField", ["f1", "f2"], function(fields){
fieldList = fields;
return fields.f1 + "-" + fields.f2
})
computedField: {
fields: ["f1", "f2"],
compute: function(fields){
fieldList = fields;
return fields.f1 + "-" + fields.f2
}
}
});

beforeEach(function(){
Expand Down Expand Up @@ -68,10 +74,13 @@ describe("computed fields", function(){
Backbone.Compute(this);
},

computedField: Backbone.Compute("computedField", ["f1", "f2"], function(fields){
fieldList = fields;
return fields.f1 + "-" + fields.f2
})
computedField: {
fields: ["f1", "f2"],
compute: function(fields){
fieldList = fields;
return fields.f1 + "-" + fields.f2
}
}
});

beforeEach(function(){
Expand All @@ -96,10 +105,13 @@ describe("computed fields", function(){
Backbone.Compute(this);
},

computedField: Backbone.Compute("computedField", ["f1", "f2"], function(fields){
fieldList = fields;
return fields.f1 + "-" + fields.f2
})
computedField: {
fields: ["f1", "f2"],
compute: function(fields){
fieldList = fields;
return fields.f1 + "-" + fields.f2
}
}
});

beforeEach(function(){
Expand Down Expand Up @@ -137,7 +149,10 @@ describe("computed fields", function(){
Backbone.Compute(this);
},

computedField: Backbone.Compute("computedField", ["f1", "f2"], handler)
computedField: {
fields: ["f1", "f2"],
compute: handler
}
});

model = new Model({
Expand Down
16 changes: 9 additions & 7 deletions src/backbone.compute.js
Expand Up @@ -8,7 +8,13 @@ Backbone.Compute = (function(Backbone, _){
// model's `initialize` function.
function initializeModel(obj){
for(var field in obj){
if (obj[field] && obj[field].computedField){
var computeAttr = obj[field];
if (computeAttr
&& computeAttr.fields
&& computeAttr.compute
){

obj[field] = computeField(field, computeAttr.fields, computeAttr.compute);
obj[field].call(obj);
}
}
Expand Down Expand Up @@ -70,12 +76,8 @@ Backbone.Compute = (function(Backbone, _){
// The raw API for computed fields. Determines whether
// you are attempting to initialize the model or define
// a computed field, and call the correct behavior.
var Compute = function(){
if (arguments.length === 1){
return initializeModel(arguments[0]);
} else {
return computeField.apply(null, arguments);
}
var Compute = function(model){
return initializeModel(model);
};

return Compute;
Expand Down

0 comments on commit 660508d

Please sign in to comment.