主干关系的hasMany最佳实践

编程入门 行业动态 更新时间:2024-10-18 12:30:09
本文介绍了主干关系的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最佳实践

本文发布于:2023-10-24 20:12:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524888.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主干   关系   hasMany

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!