自动计算EmberJS中的组件

时间:2016-10-03 17:44:23

标签: ember.js ember-data

我试图在Ember中创建一个组件,显示帖子有多少条评论。我从API中提取评论。现在的问题是,如果有新评论,它不会重新查询API。

有没有办法让Ember组件每隔15秒左右自动检查新评论以更新计数?

1 个答案:

答案 0 :(得分:3)

可以在 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);
}
相关问题