ember:在mixin上重用控制器动作的最佳方法

Chr*_*dge 11 ember.js

示例jsbin:http://jsbin.com/ICoLOgO/4/edit

如果我有一个提供动作的mixin,使用ember 1.0-rc.5,则会在没有警告的情况下调用该动作.升级到ember 1.0 final会导致弃用警告显示:

Action handlers implemented directly on controllers are deprecated in favor of action handlers on an `actions` object
Run Code Online (Sandbox Code Playgroud)

是否有更简单的方法在动作映射中公开各个动作而无需使用function.apply?

小智 27

我只是将常用操作actions放在mixin 的hash中,Ember负责将操作哈希与任何扩展mixin的控制器正确合并.

App.PaginatedListController = Ember.Mixin.create({
  queryParams: ['page'],
  page: 0,

  actions: {
    nextPage: function() {
      this.incrementProperty('page');
    },

    previousPage: function() {
      this.decrementProperty('page');
    },
  }
});

App.PostsController = Ember.ArrayController.extend(App.PaginatedListController, {
  actions: {
    // controller specific actions here
  }
});
Run Code Online (Sandbox Code Playgroud)