Skip to content

Commit

Permalink
Add .addProperty class method
Browse files Browse the repository at this point in the history
  • Loading branch information
myme committed Feb 28, 2013
1 parent 00e9aa0 commit 9d918e0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/backbonejs-computed.js
Expand Up @@ -100,6 +100,20 @@ this.Backbone.Model = (function ( Model, _ ) {

}, {

addProperty: function ( name, deps, action ) {
if ( action === undefined ) {
action = deps;
deps = [];
}

this.prototype._computedProps[ name ] = {
action: action,
deps: deps
};

return this;
},

// Override Bootstrap.Model's extend
extend: _.wrap( Model.extend, function ( extend, properties, classProperties ) {
var prop, action, propSpec, deps;
Expand Down
21 changes: 21 additions & 0 deletions test/backbonejs-computed-test.js
Expand Up @@ -144,6 +144,27 @@
assert.equals( model.get( 'oof' ), 'xuuq' );
}

},

'.addProperty (class method)': {

'is a function': function () {
var Model = Backbone.Model.extend();
assert.isFunction( Model.addProperty );
},

'returns class': function () {
var Model = Backbone.Model.extend();
assert.same( Model.addProperty(), Model );
},

'adds a new class computed property': function () {
var Model = Backbone.Model.extend();
Model.addProperty( 'foo', function () { return 10; });
var model = new Model();
assert.equals( model.get( 'foo' ), 10 );
}

}

});
Expand Down

0 comments on commit 9d918e0

Please sign in to comment.