[英]In Ember.js why does 'afterRender' not trigger on a redraw?
I'm trying to call a function when new data gets added to a table in a view, but it's not working. 我正在尝试在将新数据添加到视图中的表时调用函数,但它不起作用。 I'm using Ember.js 2.5
我正在使用Ember.js 2.5
I have two models, client and user. 我有两个模型,客户端和用户。 A client has many users.
客户有很多用户。
My view to list clients looks like this: 我列出客户端的视图如下所示:
<table class="table table-striped" id="admin-table">
<thead>
<tr>
<th>Primary User</th>
<th>User Email</th>
<th>Join Date</th>
</tr>
</thead>
<tbody>
{{#each model as |client|}}
<tr>
<td>{{client.primaryUser.name}}</td>
<td>{{client.primaryUser.email}}</td>
<td>{{client.createdAt}}</td>
</tr>
{{/each}}
</tbody>
</table>
primaryUser
is a computed property containing the first user for each client. primaryUser
是一个计算属性,包含每个客户端的第一个用户。
primaryUser: Ember.computed('users', function(){
return this.get('users.firstObject');
})
I'm using the jquery tablesorter library which adds sorting and filtering to the table. 我正在使用jquery tablesorter库,它为表添加了排序和过滤。 Because the primaryUser get's loaded by AJAX I need to call
$('#admin-table').trigger('update')
when new data is added in order to have the library index the new information. 因为AJU加载了primaryUser get,所以我需要在添加新数据时调用
$('#admin-table').trigger('update')
,以便让库索引新信息。
Here's what I'm doing: 这是我正在做的事情:
modelObserver: function() {
Ember.run.next(this, this.updateTable);
}.observes('model.@each.primaryUser')
I've tried both run.next
, and run.scheduleOnce('afterRender'..)
and the result is always the same. 我已经尝试了
run.next
和run.scheduleOnce('afterRender'..)
,结果总是一样的。 The updateTable
function triggers before all of the primaryUser objects are rendered. 该
updateTable
功能触发所有primaryUser对象的呈现之前。
Here's what happens if I put a debugger within this.updateTable
: 如果我在
this.updateTable
放置一个调试器,会发生什么:
it only triggers once, when a cached user (me) is rendered. 它只会在呈现缓存用户(我)时触发一次。 The empty cells in this table populate a few ms later when the rest of the user information is ajaxed in, but
updateTable
never re-runs. 空单元格本表中填充几毫秒后,当用户信息的其余ajaxed中,但
updateTable
永不再运行。
I can confirm at this point that the other primary user fields are not in the DOM: 我现在可以确认其他主要用户字段不在DOM中:
Is there a way to trigger an event once all objects have been rendered? 有没有办法在渲染所有对象后触发事件? I've using
Ember.run.sync()
as mentioned in this answer , but it didn't help. 我已经使用了这个答案中提到的
Ember.run.sync()
,但它没有帮助。
Everything's fine but you need to manually check if all users are rendered. 一切都很好,但你需要手动检查是否所有用户都被渲染。 There's no other option.
别无选择。 The code would look like this I think.
我认为代码看起来像这样。 It's a bit primitive, but I think you get the idea that you just can check the DOM.
它有点原始,但我认为你可以理解你可以检查DOM。
modelObserver: function() {
var areRendered = [];
Ember.$('table#admin-table tr').each(function (index, element) {
$(element).find('td').each(function (anotherIndex, anotherElement) {
if (anotherIndex <= 1) { //This is because according to your screenshot you don't have to check all the cells, just first two if I'm not mistaking.
if (anotherElement.text()) {
areRendered.push(true);
} else {
areRendered.push(false);
}
}
});
});
if (!areRendered.contains(false)) { //Checking if there were any element without data.
Ember.run.next(this, this.updateTable);
}
}.observes('model.@each.primaryUser')
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.