Skip to content

Commit

Permalink
Optimize Model#update and ensure that it ignores primitive argument…
Browse files Browse the repository at this point in the history
… values.
  • Loading branch information
Kit Cambridge committed Jul 9, 2013
1 parent b7291bd commit 049d4c9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 22 deletions.
17 changes: 7 additions & 10 deletions lib/polyclay.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,18 +354,15 @@ PolyClay.Model.prototype.propertyTypes = function()

PolyClay.Model.prototype.update = function(attr)
{
var k,
self = this,
props = Object.keys(attr),
whitelist = _.union(this.__optional, this.__properties)
;
for (var i = 0; i < props.length; i++)
if (!_.isObject(attr))
return;

var whitelist = _.union(this.__optional, this.__properties);
for (var i = 0; i < whitelist.length; i++)
{
k = props[i];
if ((whitelist.indexOf(k) !== -1) && (k !== undefined))
var k = whitelist[i];
if (_.has(attr, k))
this[k] = attr[k];
//else
// console.log('skipping unknown property ' + k);
}
this.trigger('update');
};
Expand Down
36 changes: 24 additions & 12 deletions test/test-01-polyclay.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,20 +491,32 @@ describe('polyclay', function()
struct.should.have.property(checklist[i]);
});

it('updates all known properties in update()', function()
describe('#update', function()
{
var data = {
is_valid: true,
foozles: ['three', 'four'],
count: 50,
required_prop: 'badges'
};
var obj = new Model();
obj.update(data);
it('updates all known properties', function()
{
var data = {
is_valid: true,
foozles: ['three', 'four'],
count: 50,
required_prop: 'badges'
};
var obj = new Model();
obj.update(data);

obj.required_prop.should.equal(data.required_prop);
obj.count.should.equal(data.count);
obj.is_valid.should.equal(data.is_valid);
});

obj.required_prop.should.equal(data.required_prop);
obj.count.should.equal(data.count);
obj.is_valid.should.equal(data.is_valid);
it('ignores primitive argument values', function()
{
var obj = new Model();
should.not.throw(function() { obj.update(); });
should.not.throw(function() { obj.update(null); });
should.not.throw(function() { obj.update('snozzers'); });
should.not.throw(function() { obj.update(false); });
});
});

it('emits a "change" event when a property is set', function(done)
Expand Down

0 comments on commit 049d4c9

Please sign in to comment.