Ember model reloading in interval

一世执手 提交于 2019-12-21 03:34:11

问题


I have a User model, which has latitude and longitude properties, which are used to show current user location on map.

App.User = DS.Model.extend({
    firstName: DS.attr('string'),
    lastName: DS.attr('string'),
    email: DS.attr('string'),
    jobs: DS.hasMany("App.Job"),
    latitude:   DS.attr('number'),
    longitude: DS.attr('number'),
    measuredAt: DS.attr('date'),
});

What is the best way to autoupdate latitude and longitude properties from server every given interval? Does ember-data support this kind of use case? I can't find any samples with this functionality. I think I will need to override find method for User class with setInterval in it, is this the right approach?

Thanks


回答1:


For those who are coming across this question now -- don't use setInterval, it's been shown to cause serious memory leaks.

Use Ember's own Ember.run.later instead.

See also:

  • SetTimeout vs. Ember.run.later in an ember app?
  • http://guides.emberjs.com/v1.12.0/cookbook/working_with_objects/continuous_redrawing_of_views/
  • https://www.slideshare.net/yoranbe/presentation-31170160

Edit: Feb 3, 2019 - Update broken links.




回答2:


EDIT: Please see Dmitri Zagidulin's answer.

So I figured it out. For now all I need was to override didLoad method on model, in future I will probably need more complex solution, but this is adequate for now.

didLoad: function(){
  var self = this;
  setInterval(function() {self.reload()}, 5*60*1000); //every 5 minutes
}

In case someone needs to reload more models, good solution would be to implement this as Mixin.




回答3:


The model has a .reload() method now, which you could use in a setInterval callback. See https://stackoverflow.com/a/14183507/363073

P.S. Watch #545.



来源:https://stackoverflow.com/questions/15618666/ember-model-reloading-in-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!