0

I'm trying to create a component in Ember that shows you how many comments a post has. I pull the comments from the API. The problem right now is, if there are new comments, it doesn't re-query the API.

Is there a way to have the Ember component automatically check every 15 or so seconds for new comments to update the count?

WebDevDude
  • 829
  • 1
  • 8
  • 14
  • Possible duplicate of [Model reloading with Ember Data](http://stackoverflow.com/questions/23372937/model-reloading-with-ember-data) – givanse Oct 03 '16 at 21:08

1 Answers1

3

Could call a method in init hook that triggers new comments fetching and also calls for itself when 15 sec passes.

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);
}
kristjan reinhold
  • 2,038
  • 1
  • 17
  • 34
  • this is indeed much better than the solution I provided since ember.observer() is pretty heavy. Also, it does what the OP asks which is to check every 15 s... – renno Oct 04 '16 at 15:43