[英]Ember afterRender not waiting for promises
I want to know when all content has rendered the first time, but Ember.run.scheduleOnce('afterRender'
isn't enough because there are promises in the template. 我想知道何时所有内容都已首次渲染,但是Ember.run.scheduleOnce('afterRender'
还不够,因为模板中有承诺。
{{#each entity in model.entities}}
{{entity.anotherEntity.name}}
{{/each}}
{{!-- more like above --}}
Where entities
and anotherEntities
are async relationships and promises. 其中entities
和anotherEntities
是异步关系和承诺。 The afterRender
hits before that inner content has rendered. afterRender
在内部内容呈现之前命中。 My current solution is: 我当前的解决方案是:
Ember.run.scheduleOnce('afterRender', this, function() {
Ember.RSVP.all([
this.model.get('entities').then(function(entities) {
return Ember.RSVP.all(entities.map(function(entity) {
return entity.get('anotherEntity');
}));
}),
// more like above
]).then(function() {
console.log('done-rendering');
});
});
Is there a better way to do it? 有更好的方法吗? This might not even be enough because there could be a split second between when the last promise resolves and the last section of template renders. 这甚至可能还不够,因为在最后一个承诺解析到模板渲染的最后一部分之间可能会有瞬间的间隔。 Maybe I need another afterRender
or similar check after all the promises resolve to be sure. 在所有的承诺都确定afterRender
之后,也许我需要另一个afterRender
或类似的检查。
If you're going to use the afterRender
queue, you want to schedule the call after your work is done, but before your work is rendered. 如果要使用afterRender
队列, afterRender
在完成工作之后但在呈现工作之前计划调用。 Meaning that calling it before your promise resolves is likely going to involve timing issues (as you pointed out). 这意味着在您的诺言得到解决之前调用它可能会涉及时间问题(如您所指出的)。 If I were you, I would schedule the call after your promise resolves. 如果我是您,我会在您的承诺解决后安排电话。
this.model.get('entities').then(function(entities) {
return Ember.RSVP.all(entities.map(function(entity) {
return entity.get('anotherEntity');
});
}).then(function() {
Ember.run.scheduleOnce('afterRender', this, function() {
// Your work is finished
// You can now schedule more work for after rendering
});
});
Unfortunately I don't know of a much more elegant way than this. 不幸的是,我不知道有比这更优雅的方法。 In general, the afterRender
queue isn't used very often (I only use it when wrapping third party libraries in components) so there's no reason to make it easier to use. 通常, afterRender
队列并不经常使用(我仅在将第三方库包装在组件中时才使用它),因此没有理由使其更易于使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.