Ember-Data:了解RecordArray何时完全填充 [英] Ember-Data: Knowing when a RecordArray is completely populated

查看:60
本文介绍了Ember-Data:了解RecordArray何时完全填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个情况,在DS.RecordArray上的索引更改为true,但RecordArray的内容,length属性仍然为空,此时为0,仅在以后更改。



示例代码(coffeescript):

  @set('followRequests',App.FollowRequests.find())

...

whenDataLoads:( - >

console.log @get('followRequests.isLoaded')
控制台。 log @get('followRequests.length')
@set('content',@get('followRequests')。toArray())

).observes('followRequests.isLoaded' )

第一个日志语句为真,第二个为0,使用该数据的模板为空。当我看到实际的AJAX请求时,我看到请求确​​实返回一个记录数组。并且RecordArray的长度和内容会稍后改变,如浏览器控制台所示:



App.Router.myController.get ('followRequests')get('length') ---> 12



然而,这段代码(下面)模板,但它运行12次...

  whenDataLoads:( - > 
console.log @get ('followRequests.isLoaded')
console.log @get('followRequests.length')

@set('content',@get('followRequests')toArray())

).observes('followRequests.length')

什么是正确的知道RecordArray何时完全填充的方式...?

解决方案

自从Ember.js使用承诺之后,您可以这个

  App.FollowRequests.find()。then(function(){
//这个回调将会触发数组被加载
});


I have a situation where isLoaded on a DS.RecordArray changes to true but the content, length property of the RecordArray is still empty, 0 at that time and only changes later.

Sample Code(coffeescript):

@set('followRequests', App.FollowRequests.find())

...

whenDataLoads: (->

console.log @get('followRequests.isLoaded')
console.log @get('followRequests.length')
@set('content', @get('followRequests').toArray() )

).observes('followRequests.isLoaded')

The first log statement is true while the second is 0 and the template which uses this data is empty. When I see the actual AJAX request I see that the request does return an array of records. And the length and content of the RecordArray do change some time later as seen in the Browser console by doing:

App.Router.myController.get('followRequests').get('length') ---> 12

However this code(below) does populate the content in the template, but it runs 12 times...

whenDataLoads: (->
console.log @get('followRequests.isLoaded')
console.log @get('followRequests.length')

@set('content', @get('followRequests').toArray() )

).observes('followRequests.length')

What's the right way to know when the RecordArray is completely populated...??

解决方案

Since the time Ember.js uses promises you can do this

App.FollowRequests.find().then(function () {
    // This callback will fire when array is loaded
});

这篇关于Ember-Data:了解RecordArray何时完全填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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