Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add computed def as function #31

Merged
merged 1 commit into from
Aug 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ computed: {
}
```

`computed` can also be a function returning an object.

```js
computed: function() {
return {
grossPrice: {
get: function () {
return 105;
}
}
};
}
```

Each property that declares `get` or `set` method is treated as computed.

Get the value of computed property,
Expand Down
15 changes: 13 additions & 2 deletions src/backbone.computedfields.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
Backbone.ComputedFields = (function(Backbone, _){

var _isFunction = function(obj) {
// == instead of === and || false are optimizations
// to go around nasty bugs in IE11, Safari 8 and old v8
// see underscore#isFunction
/* jshint eqeqeq: false */
return typeof obj == 'function' || false;
/* jshint eqeqeq: true */
};

var ComputedFields = function (model) {
this.model = model;
this._computedFields = [];
Expand Down Expand Up @@ -29,8 +38,10 @@ Backbone.ComputedFields = (function(Backbone, _){
},

_lookUpComputedFields: function () {
for (var obj in this.model.computed) {
var field = this.model.computed[obj];
var computed = _isFunction(this.model.computed) ? this.model.computed() : this.model.computed;

for (var obj in computed) {
var field = computed[obj];

if (field && (field.set || field.get)) {
this._computedFields.push({name: obj, field: field});
Expand Down
65 changes: 64 additions & 1 deletion test/spec/backbone.computedfields.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,69 @@ describe('Backbone.ComputedFields spec', function() {

});

describe('when ComputedFields initialized and computed is a function', function () {

var model;

beforeEach(function () {
var Model = Backbone.Model.extend({
initialize: function () {
this.computedFields = new Backbone.ComputedFields(this);
},

computed: function() {
return {
grossPrice: {
get: function () {
return 100;
}
}
};
}
});

model = new Model({ netPrice: 100, vatRate: 5});
});

it ('should be initialized', function () {
expect(model.computedFields).to.exist;
expect(model.computedFields._computedFields.length).to.equal(1);
});

it ('should access model attributes', function () {
expect(model.get('netPrice')).to.equal(100);
expect(model.get('vatRate')).to.equal(5);
});

describe('when initialize with empty', function () {
beforeEach(function () {
var Model = Backbone.Model.extend({
initialize: function () {
this.getHasBeenCalled = false;
this.computedFields = new Backbone.ComputedFields(this);
},

computed: function() {
return {
grossPrice: {
get: function () {
this.getHasBeenCalled = true;
}
}
};
}
});

model = new Model();
});

it ('should not call computed field getter', function () {
expect(model.getHasBeenCalled).to.equal(false);
});
});

});

describe('when ComputedFields are used', function () {
beforeEach(function () {
var Model = Backbone.Model.extend({
Expand Down Expand Up @@ -587,4 +650,4 @@ describe('Backbone.ComputedFields spec', function() {

});

});
});