5

我有以下路线

App.Router.map(function() {
  this.resource('projects', {path: 'projects'}, function(){
      this.route('new', {path: 'new'});
      this.route('show', {path: ':project_id'});
      this.route('edit', {path: ':project_id/edit'});
  });  
});

我希望来自“ProjectsNewController”、“ProjectsShowController”、“ProjectsEditController”的所有事件都冒泡到“ProjectsController”。

我怎样才能做到这一点?JSBin:http: //jsbin.com/ucanam/1284/edit

4

1 回答 1

15

ember 中事件冒泡的工作方式不是:

ChildController -> Controller -> ParentController

反而:

View -> Controller -> Route -> ApplicationRoute (optionally)

因此,如果从 触发事件view,它将冒泡到controller并停在那里,如果从事件处理程序controller返回true,那么它将继续冒泡到route. controller未在或 中处理的事件route将一直冒泡到ApplicationRoute.

要实现您想要做的事情,您应该使用needsAPI 来获取对 的访问权限ProjectsController并将事件/操作发送到controller使用.send(...).

例如:

App.ProjectsController = Ember.ArrayController.extend({
  actions:{
    newProject: function() {
      console.log("ProjectsController:newProject");
    }
  }
});

App.ProjectsNewController = Ember.ObjectController.extend({
  needs: ['projects'],
  actions:{
    newProject: function() {
      console.log("ProjectsNewController:newProject");
      // forward action to ProjectsController
      this.get('controllers.projects').send('newProject');
    }
  }
});

App.ProjectsEditController = Ember.ObjectController.extend({
  needs: ['projects'],
  actions:{
    newProject: function() {
      console.log("ProjectsEditController:newProject");
      // forward action to ProjectsController
      this.get('controllers.projects').send('newProject');
    }
  }
});

希望能帮助到你。

于 2013-10-02T08:32:39.050 回答