如何定期更新Ember的模型(如setInterval)? [英] How to update Ember's model periodically (such as in setInterval)?

查看:127
本文介绍了如何定期更新Ember的模型(如setInterval)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ember应用程序,其模型来自Ajax调用。第一个调用很棒,我有一个模型钩子的 Ember.Route 返回一个承诺,Ajax调用检索要显示的数据。



但是这个数据在后端经常发生变化,我想让webapp轮询服务器定期(例如每5秒钟)进行一次轮询,更新甚至更好地将模型数据完全与新检索的一个。



使用Ember.js的方法是什么?我是新来的,所以我有点迷失了。

解决方案

我认为这是一个很好的用例 Ember.run.later ,其中限制功能调用的频率。



您可以添加一个刷新到您的模型,类似于:

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


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.

解决方案

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'),
});

这篇关于如何定期更新Ember的模型(如setInterval)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆