Skip to content
This repository

Updated options fetchRelated() passed to Collection#fetch() #271

Merged
merged 1 commit into from over 1 year ago

2 participants

James Shannon Paul Uithol
James Shannon

add: true didn't seem to do anything. Collection.fetch() only checks for updated. Only if it exists, does Collection.update() get called, which then checks for add option... but it defaults to true anyways. I'm presuming that this is a bug and update: true was intended.

Additionally, I added emove: false to prevent Collection#update() from removing existing models. This is important if some models were grabbed from cache and other models relied on fetchRelated() calling fetch(). If remove != false, then the cached models would be removed

For example, if you do:

ParentCollection.reset([{id: 1, children: [{id: 1}, {id: 2}]}, {id: 2, children: [{id: 1}, {id: 2}, {id: 3}]}]);

where:

ParentCollection.model = Parent

Parent.relations = {type: hasmany, key: 'children', relatedModel: Child, createModels: false, fetchRelated: true, collectionType: Children}

Children.url = function (multipleModels) {}

Then, BB Relational will, eventually:

Create Child objects for id 1 and id 2, but not add them to the Parent[id=1].children collection, then send a Parent[id=1].children.fetch() request off. The return will be parsed and added to the collection.

Asynchronously, fetchRelated() is called for Parent[id=2], which already has children id=1, and id=2 added (though pre-fetch). It loops through and determines that only child id=3 needs to be fetched, so it sends the fetch request off. But, without update: true, then fetch() calls reset(), which will wipe everything out. Without remove: false, then fetc() calls update() which will remove all items not added, essentialy acting like reset.

James Shannon jamesshannon Updated options passed to Collection#fetch()
add: true didn't do anything, updated to update: true, which causes Collection#update() to run instead of Collection#reset()
remove: false added to prevent Collection#update() to not remove existing models. This is important if some models were grabbed from cache and other models relied on fetchRelated() calling fetch(). If remove != false, then the cached models would be removed
6fdb7c3
James Shannon

Even with this, I realized that a combination of previously-fetched (and thus cached) children and new children will screw up the order, with the new children all coming after the cached children.

I'll leave it to smarter people than myself to figure this out. (Though I can't help but think that the cache/store has a tendency to screw things up...)

Paul Uithol
Owner

add: true was indeed replaced with more granular options around Backbone 0.9.9, I think. Thanks, merging.

Paul Uithol PaulUithol merged commit 3da2d83 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 07, 2013
James Shannon jamesshannon Updated options passed to Collection#fetch()
add: true didn't do anything, updated to update: true, which causes Collection#update() to run instead of Collection#reset()
remove: false added to prevent Collection#update() to not remove existing models. This is important if some models were grabbed from cache and other models relied on fetchRelated() calling fetch(). If remove != false, then the cached models would be removed
6fdb7c3
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 1 addition and 1 deletion. Show diff stats Hide diff stats

  1. +1 1  backbone-relational.js
2  backbone-relational.js
@@ -1235,7 +1235,7 @@
1235 1235 url: setUrl
1236 1236 },
1237 1237 options,
1238   - { add: true }
  1238 + { update: true, remove: false }
1239 1239 );
1240 1240
1241 1241 requests = [ rel.related.fetch( opts ) ];

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.