减少hasMany和belongsTo查询的请求 [英] Ember fewer requests for hasMany and belongsTo lookups
问题描述
我有以下三种模式:
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个请求示例:
我希望Ember只发出一个请求,请求第一页所有必需的用户:
< b>
< / li>
handlebars文件如下所示:
{{author.firstName}}
{{#each likedBy}}
[...为简洁而删除...]
{{/ each}}
{{#each reply}}
{{author.firstName}}
{{#each likedBy}}
[..为简洁起见删除...]
{{/ each}}
{{/ each}}
{{/ each}}
我该如何做到这一点? 我知道这是一个古老的阅读,但这是一个使用 DS.RESTAdapter
的解决方案。
这很容易实现。您必须做的唯一事情是将 coalesceFindRequests
设置为 true
,如下所示:
App.ApplicationAdapter = DS.RESTAdapter.extend({
coalesceFindRequests:true
});
// 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查询的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!