Fixes #714, assign model.collection early, so that those overriding set() can use it from the get-go.

This commit is contained in:
Jeremy Ashkenas
2012-01-13 13:18:53 -05:00
parent 7c7f550a70
commit a8bd142b64
2 changed files with 13 additions and 1 deletions

View File

@@ -138,13 +138,13 @@
if (defaults = getValue(this, 'defaults')) {
attributes = _.extend({}, defaults, attributes);
}
if (options && options.collection) this.collection = options.collection;
this.attributes = {};
this._escapedAttributes = {};
this.cid = _.uniqueId('c');
this.set(attributes, {silent : true});
this._changed = false;
this._previousAttributes = _.clone(this.attributes);
if (options && options.collection) this.collection = options.collection;
this.initialize(attributes, options);
};

View File

@@ -448,4 +448,16 @@ $(document).ready(function() {
ok(attrs === models[0]);
});
test("#714: access `model.collection` in a brand new model.", 2, function() {
var col = new Backbone.Collection;
var Model = Backbone.Model.extend({
set: function(attrs) {
equals(attrs.prop, 'value');
equals(this.collection, col);
}
});
col.model = Model;
col.create({prop: 'value'});
});
});