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 Feb 26, 2015
1 parent 8264fff commit 88289db
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 @@ -1613,3 +1613,32 @@ test('#118 setOnce can be used with default string', function (t) {

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 88289db

Please sign in to comment.