Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Ensure that, on a collection, fetch calls update if only {add: true} option is passed, instead of calling reset. #1998

Closed
wants to merge 1 commit into from

2 participants

@fabiomcosta

Ensure that, on a collection, fetch calls update if only {add: true} option is passed, instead of calling reset.
Calling reset in this case looks incorrect.

@caseywebdev
Collaborator

Currently, the correct way to add only on fetch is {update: true, remove: false}. You can also use {merge: true || false} if you need to (true by default).

Think of it this way. You are bringing in data from the server and {update: true} will make sure your collection matches the response. Seems reasonable right? If you want to alter that action, tell update what you don't want to match the response. So...

add only
I don't want my response to remove or merge any models. {remove: false, merge: false}

add/merge only
I don't want my response to remove any models. {remove: false}

remove/merge only
I don't want my response to add any models, just remove old ones and update current ones. {add: false}

and any of the 2^3 = 8 combinations you can think of*

More reason I'd like update as the default method :wink:

@fabiomcosta

The problem is that fetch({add: true}) is calling calls reset, which removes all the models inside the collection. Does this look correct?

@caseywebdev
Collaborator

fetch({add: true}) isn't a valid option as of 0.9.9, the option you want is fetch({update: true, remove: false}).

@fabiomcosta

Oh OK, it's crystal clear now.
Thank you!

@caseywebdev
Collaborator

No problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 20, 2012
  1. @fabiomcosta
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 1 deletion.
  1. +1 −1  backbone.js
  2. +13 −0 test/collection.js
View
2  backbone.js
@@ -859,7 +859,7 @@
var collection = this;
var success = options.success;
options.success = function(resp, status, xhr) {
- var method = options.update ? 'update' : 'reset';
+ var method = (options.update || options.add) ? 'update' : 'reset';
collection[method](resp, options);
if (success) success(collection, resp, options);
};
View
13 test/collection.js
@@ -407,6 +407,19 @@ $(document).ready(function() {
equal(counter, 1);
});
+ test("ensure fetch calls 'update' if only {add: true} option is passed", 1, function() {
+ var collection = new Backbone.Collection;
+ var counter = 0;
+ collection.update = function(models) {
+ counter++;
+ return this;
+ };
+ collection.url = '/test';
+ collection.fetch({add: true});
+ this.syncArgs.options.success([]);
+ equal(counter, 1);
+ });
+
test("create", 4, function() {
var collection = new Backbone.Collection;
collection.url = '/test';
Something went wrong with that request. Please try again.