保持数据与服务器同步

时间:2014-02-16 16:34:24

标签: ajax node.js ember.js express

这是我的堆栈:Ember.js + Express / Node.js

假设我的端点为\posts,它将返回一个对象数组。

我有以下名为allPosts的模板:

{{#each post in content}}
 <p>{{post.body}} </p>
{{/each}}

路线:

App.AllPosts =Ember.Object.extend({
  body : null
})

App.AllPostsRoute = Ember.Route.extend({
  setupController :  function(controller,model){
    controller.set('content',model);
  } 
});

和控制器

App.AllPostsController = Ember.Controller.extend({
  actions: {
    save : fucntion(){
      // Get And update data from server via ajax
    }
  }
});

我希望数据与服务器上的数据保持同步,为此我计划使用setInterval并在每1000毫秒上方调用save操作来更新数据。但它不起作用。我像这样使用了setInterval

setInterval(App.AllPostsController.actions.save,3000);

不要想要使用Ember数据。因为数据依赖于运行服务器端的另一个节点应用程序。

1 个答案:

答案 0 :(得分:0)

您尝试对类型执行操作,而不是控制器的实例。相反,你应该在实际点击路线和控制器时开始保存,setupController是一个完成此任务的好地方。

App.AllPostsRoute = Ember.Route.extend({
  setupController :  function(controller,model){
    controller.set('content',model);  // in this code model would be blank, I'm assuming you're leaving out code
    this.startSaving(controller);
  },

  willTransition: function(transition){
    //if I'm leaving, this.stopSaving();
  },

  isSaving: false,
  startSaving: function(controller){
    this.set('isSaving', true);
    this.realSave(controller);
  },
  realSave: function(controller){
    if(!this.get('isSaving')) return;
    Em.run.later(function(){
      controller.send('save');
    }
  },
  stopSaving: function(){
    this.set('isSaving', false);
  }

});
相关问题