【问题标题】:Keeping data in-sync with server保持数据与服务器同步
【发布时间】:2014-02-16 16:34:23
【问题描述】:

这是我的堆栈: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 Data。由于数据依赖于另一个运行服务器端的 Node 应用程序。

【问题讨论】:

    标签: ajax node.js ember.js express


    【解决方案1】:

    您正在尝试对类型而不是控制器实例运行操作。相反,您应该在实际点击路由和控制器时开始保存,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);
      }
    
    });
    

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 2010-09-14
      • 2016-05-26
      • 1970-01-01
      • 2010-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多