控制器无法接收模板发送的操作

时间:2014-12-23 12:04:47

标签: ember.js

我遇到让控制器响应相应模板

中的操作的问题

控制器/日历/ show.js

import Ember from 'ember';

export default Ember.ObjectController.extend({
  queryParams: ['year', 'week'],
  year: null,
  week: null,
  incrementWeek: function() {
    var currentWeek = this.get('week');
    var nextWeek    = moment(new Date(moment().week(currentWeek + 1))).week();

    if (currentWeek > nextWeek) {
      var currentYear = moment().year();
      this.set('year', currentYear + 1);
    }

    this.set('week', nextWeek);
  },

});

模板/日历/ show.hbs

<div class="week-control pull-left">
    <a {{action "incrementWeek"}}>Next</a>
 </div>

然而,当我点击链接时,我得到了

Uncaught Error: Nothing handled the action 'incrementWeek'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.

据我所知,控制器不会返回true,所以我不确定如何从这里开始

感谢

1 个答案:

答案 0 :(得分:4)

来自ember guides

  

处理操作的路由和控制器必须放置操作处理程序   内置 actions 哈希。即使路线具有相同的方法   将name命名为动作,除非它在一个内部,否则不会被触发   动作哈希。

从'ember'导入Ember;

export default Ember.ObjectController.extend({
  queryParams: ['year', 'week'],
  year: null,
  week: null,

  actions: {
    incrementWeek: function() {
      // ...
    }
  }
});