From 34ce37137ea91af1d6e912c075e3c3e35b1788ad Mon Sep 17 00:00:00 2001 From: Casey Foster Date: Thu, 1 Nov 2012 17:00:59 -0700 Subject: [PATCH 1/2] Fix #1791 reorder constructor so `attributes` is defined for `parse` --- backbone.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/backbone.js b/backbone.js index 8953d47c..cb6412a2 100644 --- a/backbone.js +++ b/backbone.js @@ -185,16 +185,16 @@ var defaults; var attrs = attributes || {}; if (options && options.collection) this.collection = options.collection; - if (options && options.parse) attrs = this.parse(attrs); - if (defaults = _.result(this, 'defaults')) { - attrs = _.extend({}, defaults, attrs); - } this.attributes = {}; this._escapedAttributes = {}; this.cid = _.uniqueId('c'); this.changed = {}; this._changes = {}; this._pending = {}; + if (options && options.parse) attrs = this.parse(attrs); + if (defaults = _.result(this, 'defaults')) { + attrs = _.extend({}, defaults, attrs); + } this.set(attrs, {silent: true}); // Reset change tracking. this.changed = {}; From 79093411397f4e329a46191d2fa08d2e7c4b8cf9 Mon Sep 17 00:00:00 2001 From: Casey Foster Date: Thu, 1 Nov 2012 17:09:05 -0700 Subject: [PATCH 2/2] Add test for #1791 --- test/model.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/model.js b/test/model.js index 28346984..c962de4b 100644 --- a/test/model.js +++ b/test/model.js @@ -889,4 +889,12 @@ $(document).ready(function() { deepEqual(changes, ['a',1,'item']); }); + test("#1791 - `attributes` is available for `parse`", function() { + var Model = Backbone.Model.extend({ + parse: function() { this.has('a'); } // shouldn't throw an error + }); + var model = new Model(null, {parse: true}); + expect(0); + }); + });