减少hasMany和belongsTo查询的请求

编程入门 行业动态 更新时间:2024-10-25 14:23:27
本文介绍了减少hasMany和belongsTo查询的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下三种模式:

ConversationApp.Post = DS.Model.extend( body: DS.attr()回复:DS.hasMany('reply',async:true)作者:DS.belongsTo('user',async:true)赞了:DS.hasMany ('user',async:true)) ConversationApp.Reply = DS.Model.extend( body:DS.attr() post:DS .belongsTo('post') author:DS.belongsTo('user',async:true) likeBy:DS.hasMany('user',async:true)) ConversationApp.User = DS.Model.extend( firstName:DS.attr() lastName:DS.attr())

我的索引路由会调用这个函数:

ConversationApp.IndexRoute = Em.Route.extend( model:(args) - > @ store.find('post',page:1)#=> / api / v1 / posts?page = 1 )

接通电话后,Ember开始提取第一页所需的所有用户 - 第一页共有17(!)个不同的用户请求(有10个帖子)。以下是Ember对服务器提出的3个请求示例:

  • / api / v1 / users / 11375
  • / api / v1 / users / 4383
  • / api / v1 / users?id [] = 34588& ids [] = 7442& ids [] = 10294

我希望Ember只发出一个请求,请求第一页所有必需的用户:

< b> < / li>

handlebars文件如下所示:

我该如何做到这一点? 我知道这是一个古老的阅读,但这是一个使用 DS.RESTAdapter 的解决方案。

这很容易实现。您必须做的唯一事情是将 coalesceFindRequests 设置为 true ,如下所示:

App.ApplicationAdapter = DS.RESTAdapter.extend({ coalesceFindRequests:true });

或者如果您使用> Ember CLI

// app / adapters / application.js 从'烬数据'; 导出默认值DS.RESTAdapter.extend({ coalesceFindRequests:true });

您可以在 DS.RESTAdapter api文档

祝您好运! :)

I have the following three models:

ConversationApp.Post = DS.Model.extend( body: DS.attr() replies: DS.hasMany('reply', async: true) author: DS.belongsTo('user', async: true) likedBy: DS.hasMany('user', async: true) ) ConversationApp.Reply = DS.Model.extend( body: DS.attr() post: DS.belongsTo('post') author: DS.belongsTo('user', async: true) likedBy: DS.hasMany('user', async: true) ) ConversationApp.User = DS.Model.extend( firstName: DS.attr() lastName: DS.attr() )

And my index route makes this call:

ConversationApp.IndexRoute = Em.Route.extend( model: (args) -> @store.find('post', page: 1) # => /api/v1/posts?page=1 )

After that call is made, Ember starts fetching all the users needed for the first page - a total of 17(!) different requests for users on the first page (with 10 posts). Here are 3 examples of the requests Ember makes to the server:

  • /api/v1/users/11375
  • /api/v1/users/4383
  • /api/v1/users?ids[]=34588&ids[]=7442&ids[]=10294

I would like Ember to only make one request, that requests all the required users for the first page:

  • /api/v1/users?ids[]=34588&ids[]=7442&ids[]=10294&ids[]=11375&ids[]=4383

The handlebars file looks like this:

{{#each}} {{author.firstName}} {{#each likedBy}} [... removed for brevity ...] {{/each}} {{#each replies}} {{author.firstName}} {{#each likedBy}} [... removed for brevity ...] {{/each}} {{/each}} {{/each}}

How can I accomplish that?

解决方案

I know that this is an old thread, but here is a solution using the DS.RESTAdapter.

It's pretty easy to accomplish this. The only thing you have to do is set coalesceFindRequests to be true, like so:

App.ApplicationAdapter = DS.RESTAdapter.extend({ coalesceFindRequests: true });

or if you are using Ember CLI

// app/adapters/application.js import DS from 'ember-data'; export default DS.RESTAdapter.extend({ coalesceFindRequests: true });

You can read more about it in the DS.RESTAdapter api docs

Good luck! :)

更多推荐

减少hasMany和belongsTo查询的请求

本文发布于:2023-10-21 17:21:46,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:hasMany   belongsTo

发布评论

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

>www.elefans.com

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