mirror of
https://github.com/jashkenas/backbone.git
synced 2026-04-08 03:00:26 -04:00
Fix for adding models with custom set methods to collections, issue #539
This commit is contained in:
@@ -568,7 +568,7 @@
|
||||
if (!(model instanceof Backbone.Model)) {
|
||||
var attrs = model;
|
||||
model = new this.model(attrs, {collection: this});
|
||||
if (model.validate && !model._performValidation(attrs, options)) model = false;
|
||||
if (model.validate && !model._performValidation(model.attributes, options)) model = false;
|
||||
} else if (!model.collection) {
|
||||
model.collection = this;
|
||||
}
|
||||
|
||||
@@ -44,6 +44,33 @@ $(document).ready(function() {
|
||||
equals(col.get(101), model);
|
||||
});
|
||||
|
||||
test("Collection: add model with attributes modified by set", function() {
|
||||
var CustomSetModel = Backbone.Model.extend({
|
||||
defaults: {
|
||||
number_as_string: null //presence of defaults forces extend
|
||||
},
|
||||
|
||||
validate: function (attributes) {
|
||||
if (!_.isString(attributes.num_as_string)) {
|
||||
return 'fail';
|
||||
}
|
||||
},
|
||||
|
||||
set: function (attributes, options) {
|
||||
if (attributes.num_as_string) {
|
||||
attributes.num_as_string = attributes.num_as_string.toString();
|
||||
}
|
||||
Backbone.Model.prototype.set.call(this, attributes, options);
|
||||
}
|
||||
});
|
||||
|
||||
var CustomSetCollection = Backbone.Collection.extend({
|
||||
model: CustomSetModel
|
||||
});
|
||||
var col = new CustomSetCollection([{ num_as_string: 2 }]);
|
||||
equals(col.length, 1);
|
||||
});
|
||||
|
||||
test("Collection: update index when id changes", function() {
|
||||
var col = new Backbone.Collection();
|
||||
col.add([
|
||||
|
||||
Reference in New Issue
Block a user