fetchRelated makes assumption that I don't think holds #80

Closed
jclyons opened this Issue Feb 22, 2012 · 4 comments

4 participants

@jclyons

I'm using a One-to-Many relation. My back-end is RequestTracker's REST interface (http://requesttracker.wikia.com/wiki/REST). My model and respective collection are called Ticket and HistoryItemCollection. Basically a HistoryItemCollection is the entire History for a given Ticket object.

In fetchRelated a comment says the following:

// An assumption is that when 'Backbone.Collection.url' is a function, it can handle building of set urls.
// To make sure it can, test if the url we got by supplying a list of models to fetch is different from
// the one supplied for the default fetch action (without args to 'url').

I guess I don't understand why this is a safe assumption. For instance, in my case, my REST backend (RequestTracker) only supports getting the entire collection at the same time. I cannot construct a single URL that gets a set of individual models. However, this doesn't bother me that much, since I can still get the entire History of a Ticket without having to fetch each individual item, and then disregard the items I don't need. So, essentially, the collection.url() for fetching the entire collection and the ur() for fetching a set, ought to be the same. However, Backbone-relational only uses the url when the set url is different from the collection url, and disregards the url altogether otherwise, opting instead- I think - to fetch the models one at a time.

This also creates problems when I am trying to fetch the collection manually, since my backbone-sync override passes no arguments to my collection.url(), even though I still want to fetch the entire collection.

Can you explain more why this should be assumed?

@grumpit

I came up with a completely untested hack for this, which I am probably going to end up using. I'm not needing the ability to add/remove items - in my case all I need is to get the data into the app and present it to the user, but I've added some comments in the code to show the thinking and how it might be made useful.

https://gist.github.com/3195852

@grumpit

I updated the gist above and have it working in my app. There's certainly room for improvement, but it allows a collection to be fetched as a whole and fires a reset event.

https://gist.github.com/3195852

@geoffharcourt

Rails nested resources are also set to pull whole collections with a single url, an example being /posts/23/comments would be all the comments that belong to post #23. @grumpit, your approach seems to get around this issue (where URLs might be build by collection). Since the association name would probably in most cases be the nested URL suffix, could we construct the URL by using the current model's URL + /key ? Then we would have one configuration option ("nestedResource: true"), and maybe a parameter to fetchRelated to force a fetch of parameters even if they weren't in the parent object's JSON.

I have situations in my application where the child data is expensive to fetch from the DB, and I'd rather not pull it except when it's needed. I don't want to pump out all of the ID numbers in the post JSON, so if I could start with an empty collection and then call something like /posts/23/comments and populate that JSON that would dovetail really nicely with how RoR handles nested URLs.

@DouweM
Collaborator

@geoffharcourt So what you're proposing is a nestedResource option that would make the relation's collection's url be the current model's URL + "/" + key? I would actually like that and I think there are enough people who use Relational with Rails to make it suitable for inclusion in the core project.

Could you post a pull request or at least a dedicated issue?

@DouweM DouweM closed this Dec 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment