简体   繁体   English

在Ember.js 2.3中,如何将hasMany异步调用编译为ember中的一个调用,而不是多个?

[英]in Ember.js 2.3, how do I compile a hasMany async call into one call in ember instead of several?

I'm upgrading to ember-cli and ember 2.3. 我正在升级到ember-cli和ember 2.3。 Say I have a model called User and a model called Post , and a user ... 说我有一个名为User的模型和一个称为Post的模型,以及一个用户...

posts: DS.hasMany('post', {async:true})

Now, this works the way I expect it to, lazily loading data and not loading posts unless it is required in either the .js or the template. 现在,这将按我期望的方式工作,即延迟加载数据而不加载帖子,除非.js或模板中要求这样做。 So when I do 所以当我这样做

{{#each user.posts as |post|}}
  {{post.title}}
{{/each}}

I get each post to render its title without a problem. 我得到每个帖子都可以毫无问题地呈现其标题。 However, in my server logs, I see this: 但是,在我的服务器日志中,我看到以下内容:

GET /posts/2
GET /posts/7
GET /posts/13

where the numbers are the post ids. 数字是帖子ID。 This is to be expected, as when I return a user instance from the server, I return a list of the ids as the parameter 'posts'. 这是可以预期的,因为当我从服务器返回用户实例时,我将返回ID列表作为参数“ posts”。 So the user instance has: 因此,用户实例具有:

...
'posts': '2,7,13'
...

in its data. 在其数据中。

Now my question is this: way back when, when I used ember-data 1.0 (pre ember-cli and pre ember 1.13), I remember this call being made to the database instead for the same use case: 现在我的问题是:回到当我使用ember-data 1.0(pre ember-cli和pre ember 1.13)时,我记得针对同一用例对数据库进行了此调用:

GET /posts?ids=2&7&13

or something along that line. 或类似的东西。 I can't remember the exact format, but then, I could access the list of ids on the server side using this line of code: 我不记得确切的格式,但是随后,我可以使用以下代码行访问服务器端的ID列表:

var ids = req.query.ids.toString();

which gave me a comma separated list of ids (in string format). 这给了我一个逗号分隔的ID列表(字符串格式)。 I would then convert this into the sql statement 然后,我将其转换为sql语句

SELECT * from posts where id in (2,7,13)

This SQL call was interpreted as a manyArray, I think, on the Ember Side and easily behaved as you would expect an Ember Array would. 我认为,此SQL调用在Ember方面被解释为manyArray,并且行为简单,就像您期望的Ember Array一样。

How can I get this to happen again? 我怎样才能再次发生这种情况? I am quite confident that I am missing something and that I don't have to 'hack' ember-data; 我非常有信心,我丢失了一些东西,不必“破解”余烬数据。 I would very much like to compress these calls into one instead of having an individual call to the database for each 'post'. 我非常想将这些调用压缩为一个,而不是为每个“发布”单独调用数据库。

I should also mention that I am not looking to make {async:false} for these calls. 我还应该提到,我不想为这些调用设置{async:false}。

I think the thing you are looking for is coalesceFindRequests , this is a setting on your Adapter to tell Ember to bunch multiple requests that happen in the same runloop into one GET request as you had in the past. 我认为您正在寻找的东西是coalesceFindRequests ,这是您适配器上的一项设置,用于告诉Ember与过去一样将在同一运行循环中发生的多个请求打包为一个GET请求。

You can see more detail here but essentially all you need to do is add the following to either your ApplicationAdapter (to enable it for all requests for all types) or to your posts adapter (so that it only affects the post requests) 您可以在此处看到更多详细信息但实际上,您所需要做的就是将以下内容添加到ApplicationAdapter(以对所有类型的所有请求启用它)或发布适配器(以使其仅影响发布请求)

Here is an example if you are using pod structure for your files (which I recommend) 这是一个示例,如果您使用文件的pod结构(我建议)

// app/application/adapter.js
import DS from 'ember-data';

export default DS.JSONAPIAdapter.extend({
  coalesceFindRequests: true
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM