From b347f9f8d4df0987e263a22ddcba34ae65f73bca Mon Sep 17 00:00:00 2001 From: Sam Breed Date: Tue, 14 Aug 2012 14:16:04 -0600 Subject: [PATCH] consistently handle `undefined` being passed to the model constr (#1545) --- backbone.js | 6 +++--- test/model.js | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/backbone.js b/backbone.js index 418fc7d2..ada7a0ff 100644 --- a/backbone.js +++ b/backbone.js @@ -183,11 +183,11 @@ // is automatically generated and assigned for you. var Model = Backbone.Model = function(attributes, options) { var defaults; - attributes || (attributes = {}); + var attrs = attributes || {}; if (options && options.collection) this.collection = options.collection; if (options && options.parse) attributes = this.parse(attributes); if (defaults = _.result(this, 'defaults')) { - attributes = _.extend({}, defaults, attributes); + attrs = _.extend({}, defaults, attrs); } this.attributes = {}; this._escapedAttributes = {}; @@ -195,7 +195,7 @@ this.changed = {}; this._silent = {}; this._pending = {}; - this.set(attributes, {silent: true}); + this.set(attrs, {silent: true}); // Reset change tracking. this.changed = {}; this._silent = {}; diff --git a/test/model.js b/test/model.js index a121d698..4a8d2449 100644 --- a/test/model.js +++ b/test/model.js @@ -816,4 +816,15 @@ $(document).ready(function() { strictEqual(model.save(), false); }); + test("#1545 - `undefined` can be passed to a model constructor without coersion", function() { + var Model = Backbone.Model.extend({ + defaults: { one: 1 }, + initialize : function(attrs, opts) { + equal(attrs, undefined); + } + }); + var emptyattrs = new Model(); + var undefinedattrs = new Model(undefined); + }); + });