在 EmberJS 中自动计算组件
Auto Calculate Component in EmberJS
我正在尝试在 Ember 中创建一个组件,显示 post 有多少条评论。我从 API 中提取评论。现在的问题是,如果有新的评论,它不会重新查询API。
有没有办法让 Ember 组件每 15 秒左右自动检查一次新评论以更新计数?
可以在 init 挂钩中调用一个方法来触发新的评论获取,并在 15 秒后调用自身。
commentsCount: Ember.computed.alias('comments.length'), // Use in template for items count
init: function() {
this._super(...arguments);
this.getNewComments();
},
getNewComments: function() {
Ember.run.later(() => {
this.get('store').query('comments', { post: this.get('post.id') }).then(newItems => {
this.get('comments').pushObjects(newItems);
this.getNewComments(); // Calls itself out
});
}, 15000);
}
我正在尝试在 Ember 中创建一个组件,显示 post 有多少条评论。我从 API 中提取评论。现在的问题是,如果有新的评论,它不会重新查询API。
有没有办法让 Ember 组件每 15 秒左右自动检查一次新评论以更新计数?
可以在 init 挂钩中调用一个方法来触发新的评论获取,并在 15 秒后调用自身。
commentsCount: Ember.computed.alias('comments.length'), // Use in template for items count
init: function() {
this._super(...arguments);
this.getNewComments();
},
getNewComments: function() {
Ember.run.later(() => {
this.get('store').query('comments', { post: this.get('post.id') }).then(newItems => {
this.get('comments').pushObjects(newItems);
this.getNewComments(); // Calls itself out
});
}, 15000);
}