ember-data findAll在第一个请求时返回0个记录 [英] ember-data findAll returns 0 records on first request

查看:118
本文介绍了ember-data findAll在第一个请求时返回0个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我目前正在使用修订版13的ember数据。

>

目前我正在ApplicaitonRoute中尝试:

  App.ApplicationRoute = Ember.Route.extend 
setupController: - >
App.Contact.find()。then(contacts) - >
console.log contacts.get('length')

我发现是上面的console.log语句将记录0,但是如果我输入:

  App.Contact.find()。get('长度')

进入控制台,长度是我预期的,即大于0.



我对于发生了什么感到困惑,我认为承诺在记录实现之前不会得到解决。



我已经经历了代码,json在序列化程序中被解析出来,我无法解释为什么第一次为0的长度。



我不能为此创建一个小提琴,因为只有通过休息适配器连接到我们的后端商店时才会发生。

解决方案

在github上创建一个问题后,我得到了这个答案:



实际上,DS.Model.find()返回一个'promistified'RecordArray,他的记录库被加载(即它的是真实的属性)。问题是DS.Model.find()在数组创建后立即加载,因此承诺得到解决。



为了解决您的问题(因为我想你想返回一个活动数组),这样的东西应该可以工作:

  App.Contact.find({ })然后(contacts) - > 
return App.Contact.all()

这样,在引擎盖下,你是调用findQuery,当后端返回记录时加载,而不是在数组创建


Where is the best place to boot strap ember-data in order to fill the store:

I am currently using revision 13 of ember-data.

At the moment I am trying this in the ApplicaitonRoute:

App.ApplicationRoute = Ember.Route.extend
  setupController: ->
    App.Contact.find().then (contacts) ->
      console.log contacts.get('length')

What I am finding is that the console.log statement above will log 0 but if I then type:

App.Contact.find().get('length')

into the console, the length is as I would expect, i.e. greater than 0.

I am confused as to what is going on, I would have thought that the promise would not be resolved until the records are materialized.

I've gone through the code and the json is parsed out in the serializer and I cannot work out why the length is 0 the first time.

I can't create a fiddle for this as it only happens when connecting to our backend store through the rest adapter.

解决方案

I got this answer after creating an issue on github:

Actually, DS.Model.find() return a 'promistified' RecordArray whch promise is resolved when the recordarray is loaded (ie its isLoaded propery is true). The problem is that DS.Model.find() is loaded as soon as the array is created, as a consequence the promise is resolved.

In order to fix your problem (because I think you want to return a live array), something like this should work:

App.Contact.find({}).then(contacts) ->
   return App.Contact.all()

This way, under the hood, you are calling a findQuery, which is loaded when the backend returns the records, not at the array creation

这篇关于ember-data findAll在第一个请求时返回0个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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