主干关系的hasMany最佳实践 [英] Backbone-relational hasmany best practices

查看:149
本文介绍了主干关系的hasMany最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的骨干关系,我不知道什么是使用的hasMany正确的方式。

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

我有一个其中有许多孩子(由多我的意思是成千上万的儿童)模型。为了避免性能问题,我查询的孩子通过自己的外键: /父/子= 而不是1,创造了巨大的 child_ids的名单? 。但是,看来这是不一样骨干关系的工作。

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,改变我的JSON API,包括在父子ID列表,然后发送成千上万的ID为骨架的关系建议:

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,覆盖骨干关系很多方法,让它处理这种情况。我首先想到的是:

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,仅使用骨干关系作为存储,然后用收集来管理关系。

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

4,其他一些神奇的方法或模式或骨干框架

4, Some other magic way or pattern or backbone framework

感谢您的帮助。

推荐答案

下面是我对我目前的项目进展的解决方案。需要注意的是项目的hasMany评论,事件,文件和视频。这些关系和他们的反向关系在这些模型中定义:

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 }) });
    }
});

我已经配置采取充当连续的WHERE条款参数REST端点。因此, project.updateRelation('意见')将发送给请求/评论?PROJECT_ID = 4 我有一些进一步在服务器端逻辑过滤掉的东西,用户无权看到的。 (Laravel后端,顺便说一句)

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)

这篇关于主干关系的hasMany最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆