0

在我的应用程序中,我有模型,我想在模型更新时向服务器发送一个 ajax 请求。例如我有一个这样的模型:

App.Post = DS.Model.extend({
  title:DS.attr('string'),
  comment:DS.attr('string')
});

和这样的控制器:

App.PostController = Ember.Controller.extend({
   //if post model update send Ajax Request 
});
App.CommentController = Ember.Controller.extend({
  //if post model update send ajax Request
});
App.OtherController = Ember.Controller.extend({
 //if post model update send ajax Request
});
4

1 回答 1

0

你可以在路由中定义一个动作

App.ApplicationRoute = Ember.Route.extend({
    actions:{
        sendRequest:function(){
            //send Ajax Request
        }
    }
});

并在您的帖子路由的 setupController 挂钩中订阅模型事件

App.PostRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    controller.set('model', model);
    model.on('didUpdate',controller,function(){this.send('sendRequest')})
    model.on('didCreate',controller,function(){this.send('sendRequest')})
    model.on('didDelete',controller,function(){this.send('sendRequest')})
  }
});

希望对您有所帮助,这只是一个示例,您可以通过多种方式进行操作,其想法是订阅在服务器响应 POST、PUT 或 DELETE 请求时触发的模型事件

基于你的 JsBin....

OlapApp.OlapRoute = Ember.Route.extend({
    setupController(controller,model){
    var that = this;
      this.store.findAll('axisModel').then(function(axisModel){
        axisModel.on('didUpdate',controller,function(){this.send('sendRequest')})
        axisModel.on('didCreate',controller,function(){this.send('sendRequest')})
        axisModel.on('didDelete',controller,function(){this.send('sendRequest')})
        that.controllerFor('OlapColumn').set('model', axisModel);
        that.controllerFor('OlapRow').set('model', axisModel);
        that.controllerFor('OlapFilter').set('model', axisModel);
        });
    }
});
于 2013-10-14T11:26:16.407 回答