Skip to content
Open
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
5 changes: 4 additions & 1 deletion ampersand-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function Base(attrs, options) {
this._events = {};
this._values = {};
this._definition = Object.create(this._definition);
if (options.parse) attrs = this.parse(attrs, options);
this.parent = options.parent;
this.collection = options.collection;
this._keyTree = new KeyTree();
Expand Down Expand Up @@ -114,6 +113,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();
});