Skip to content

Commit

Permalink
Allow passing parse: true to set
Browse files Browse the repository at this point in the history
  • Loading branch information
jackboberg committed Dec 9, 2014
1 parent 935c12a commit a105cdc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ _.extend(Base.prototype, BBEvents, {

options = options || {};

if (options.parse) {
attrs = this.parse(attrs, options);
}

if (!this._validate(attrs, options)) return false;

// Extract attributes and options.
Expand Down
29 changes: 29 additions & 0 deletions test/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -1509,3 +1509,32 @@ test('#68, #110 mixin props should not be deleted', function (t) {
t.ok(sprocket.selected);
t.end();
});

test('#106 #84 - set accepts options.parse', function (t) {
var me;
var Person = State.extend({
props: {
id: 'number',
name: 'string'
},
parse: function (attrs) {
attrs.id = attrs.personID;
delete attrs.personID;
return attrs;
}
});

me = new Person();
me.set({ personID: 123, name: 'Phil' });
t.equal(me.id, undefined, 'does not parse attributes if not passed');

me = new Person();
me.set({ personID: 123, name: 'Phil' },{ parse: false });
t.equal(me.id, undefined, 'does not parse attributes if options.parse is false');

me = new Person();
me.set({ personID: 123, name: 'Phil' },{ parse: true });
t.equal(me.id, 123, 'parses attributes if options.parse is true');

t.end();
});

0 comments on commit a105cdc

Please sign in to comment.