Backbone-relational hasmany best practices
Asked Answered
D

1

11

I am new to Backbone-relational, I am not sure what is the right way to use HasMany.

I have a Parent model which have many children (by "many" I mean thousands of children). In order to avoid performance issue, I query children by their foreign key: /child/?parent=1, instead of create a huge list of child_ids in Parent. But seems this is not the way Backbone-relational work.

So I am wondering what is the right way to handle this.

1, Change my json api to include list of child id in parent, then send thousands of ids as Backbone-relational recommend:

url = function(models) {
  return '/child/' + ( models ? 'set/' + _.pluck( models, 'id' ).join(';') + '/' : '');
}
// this will end up with a really long url: /child/set/1;2;3;4;...;9998;9999

2, override many method in Backbone-relational, let it handle this situation. My first thought is :

relations: [{
  collectionOptions: function(model){
    // I am not sure if I should use `this` to access my relation object 
    var relation = this;
    return {
      model: relation.relatedModel,
      url: function(){
        return relation.relatedModel.urlRoot + '?' + relation.collectionKey + '=' + model.id;
      }
    }
  }
}]
// This seems work, but it can not be inherent by other model
// And in this case parent will have am empty children list at beginning.    
// So parent.fetchRelated() won't fetch anything, I need call this url my self.

3, Only use Backbone-relational as a Store, then use Collection to manage relations.

4, Some other magic way or pattern or backbone framework

Thanks for help.

Desquamate answered 11/4, 2013 at 16:46 Comment(4)
In my experience with backbone-relational, I don't think its going to perform well with thousands of models. I've had some significant performance issues with hundreds of models - especially when loading them. Backbone-relational is quite "chatty" with firing events, and depending on how many properties you have in your model, it ends up being dozens of events fired per model times thousands of models.Travax
I'm curious how you eventually handled this.Hilton
this is the way I eventually use, not very clean, but works so far.Desquamate
If you know you need to fetch all, I'd go for the inverse relationship (a child knows his parent) and then fetch all children with a separate collection which implicitly fills up the backbone relational store and therefore builds up your hierarchy with a single call to the server.Protozoan
R
1

Here's the solution that I have going on my current project. Note that Project hasMany comments, events, files, and videos. Those relations and their reverse relations are defined on those models:

Entities.Project = Backbone.RelationalModel.extend({
    updateRelation: function(relation) {
        var id = this.get('id'),
            collection = this.get(relation);

        return collection.fetch({ data: $.param({ project_id: id }) });
    }
});

I have the REST endpoint configured to take parameters that act as successive "WHERE" clauses. So project.updateRelation('comments') will send a request to /comments?project_id=4 I have some further logic on the server-side to filter out stuff the user has no right to see. (Laravel back-end, btw)

Recliner answered 10/12, 2013 at 19:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.