Fixes #1843 -- reworks parsing behavior. It now always applies if you define it, and no longer passes xhr. (parsing should be a pure function of the data)

This commit is contained in:
Jeremy Ashkenas
2012-11-28 14:26:49 -05:00
parent 3de9f3504e
commit fbbb2e675e
2 changed files with 9 additions and 24 deletions

View File

@@ -191,7 +191,7 @@
this.changed = {};
this._changes = {};
this._pending = {};
if (options && options.parse) attrs = this.parse(attrs);
if (this.parse) attrs = this.parse(attrs);
if (defaults = _.result(this, 'defaults')) {
attrs = _.extend({}, defaults, attrs);
}
@@ -344,7 +344,8 @@
var model = this;
var success = options.success;
options.success = function(resp, status, xhr) {
if (!model.set(model.parse(resp, xhr), options)) return false;
if (model.parse) resp = model.parse(resp);
if (!model.set(resp, options)) return false;
if (success) success(model, resp, options);
};
return this.sync('read', this, options);
@@ -386,7 +387,7 @@
var success = options.success;
options.success = function(resp, status, xhr) {
done = true;
var serverAttrs = model.parse(resp, xhr);
var serverAttrs = model.parse ? model.parse(resp) : resp;
if (options.wait) serverAttrs = _.extend(attrs || {}, serverAttrs);
if (!model.set(serverAttrs, options)) return false;
if (success) success(model, resp, options);
@@ -441,12 +442,6 @@
return base + (base.charAt(base.length - 1) === '/' ? '' : '/') + encodeURIComponent(this.id);
},
// **parse** converts a response into the hash of attributes to be `set` on
// the model. The default implementation is just to pass the response along.
parse: function(resp, xhr) {
return resp;
},
// Create a new model with identical attributes to this one.
clone: function() {
return new this.constructor(this.attributes);
@@ -569,8 +564,8 @@
this._reset();
this.initialize.apply(this, arguments);
if (models) {
if (options.parse) models = this.parse(models);
this.reset(models, {silent: true, parse: options.parse});
if (this.parse) models = this.parse(models);
this.reset(models, {silent: true});
}
};
@@ -774,11 +769,11 @@
// models to the collection instead of resetting.
fetch: function(options) {
options = options ? _.clone(options) : {};
if (options.parse === void 0) options.parse = true;
var collection = this;
var success = options.success;
options.success = function(resp, status, xhr) {
collection[options.add ? 'add' : 'reset'](collection.parse(resp, xhr), options);
if (collection.parse) resp = collection.parse(resp);
collection[options.add ? 'add' : 'reset'](resp, options);
if (success) success(collection, resp, options);
};
return this.sync('read', this, options);
@@ -802,12 +797,6 @@
return model;
},
// **parse** converts a response into a list of models to be added to the
// collection. The default implementation is just to pass it through.
parse: function(resp, xhr) {
return resp;
},
// Create a new collection with an identical list of models as this one.
clone: function() {
return new this.constructor(this.models);

View File

@@ -370,16 +370,12 @@ $(document).ready(function() {
equal(undefined, e.collection);
});
test("fetch", 4, function() {
test("fetch", 2, function() {
var collection = new Backbone.Collection;
collection.url = '/test';
collection.fetch();
equal(this.syncArgs.method, 'read');
equal(this.syncArgs.model, collection);
equal(this.syncArgs.options.parse, true);
collection.fetch({parse: false});
equal(this.syncArgs.options.parse, false);
});
test("create", 4, function() {