Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an Ember application whose model comes from an Ajax call. The first call works great, I have the model hook of the Ember.Route return a promise to the Ajax call that retrieves the data to be displayed.

But this data changes frequently in the backend, and I want to have the webapp poll the server periodically, say, every 5 seconds, and update or, even better, swap the model data entirely with the newly retrieved one.

What is the appropriate way of doing that with Ember.js? I’m new to Ember so I’m a bit lost with this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
204 views
Welcome To Ask or Share your Answers For Others

1 Answer

I think this is a good use case for Ember.run.later, which limits the frequency of function calls.

You could just add a refresh to your model, similar to this:

App.Model = DS.Model.extend({
   poll: function() {
      var _this = this;
      Ember.run.later( function() {
         _this.reload(); 
         _this.poll();
      }, 500);
   }.observes('didLoad'),
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...