From 88289db083b49b9f9aaf04a857b29ae49d5324b5 Mon Sep 17 00:00:00 2001 From: Jack Boberg Date: Wed, 25 Feb 2015 20:20:34 -0500 Subject: [PATCH] Allow passing `parse: true` to set --- ampersand-state.js | 4 ++++ test/full.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/ampersand-state.js b/ampersand-state.js index 5f587de..35573d1 100644 --- a/ampersand-state.js +++ b/ampersand-state.js @@ -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. diff --git a/test/full.js b/test/full.js index 52c6dd0..3b7961e 100644 --- a/test/full.js +++ b/test/full.js @@ -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(); +});