2

我正在开发我的第一个 Ember.js 应用程序,虽然我有一个模板加载,但当我尝试调用我在控制器中定义的操作时,我收到一个错误:“未捕获的错误:没有处理事件'showNew'。” 我不确定我是否错误地设置了路线和控制器,或者我是否遗漏了其他东西。

./router.js:

Seanchai.Router.map(function(){
  this.resource("stories", function(){
    this.route('new');
  });
});

Seanchai.StoriesRoute = Ember.Route.extend({
  model: function(){
    Seanchai.Story.find();
  }
});


Seanchai.Router.reopen({
  location: 'history'
});

./controllers/stories_controller.js:

Seanchai.StoriesController = Ember.ArrayController.extend({    

  showNew: function() {
    this.set('isNewVisible', true);
  }
});

./templates/stories/index.hbs:

<table>
  <thead>
  <tr>
    <th>ID</th>
    <th>Name</th>
  </tr>
  </thead>
  <tbody>
    {{#each stories}}
      {{view Seanchai.ShowStoryView storyBinding="this"}}
    {{/each}}
    {{#if isNewVisible}}
      <tr>
        <td>*</td>
        <td>
          Test
        </td>
      </tr>
    {{/if}}
    </tbody>
</table>
<div class="commands">
  <a href="#" {{action showNew}}>New Story</a>
</div>

如果我将操作移动到路由器中,就像这样,它可以工作,但根据文档,我应该能够在控制器中执行此操作。

更新了./router.js:

Seanchai.Router.map(function(){
  this.resource("stories", function(){
    this.route('new');
  });
});

Seanchai.StoriesRoute = Ember.Route.extend({
  model: function(){
    Seanchai.Story.find();
  },
  events: {
    showNew: function() {
      this.set('isNewVisible', true);
    }
  }
});


Seanchai.Router.reopen({
  location: 'history'
});

我显然错过了一些东西,但我不确定是什么。

4

2 回答 2

3

我猜你的showNew事件没有在控制器上触发,因为你有一个stories.index模板,所以你应该挂钩到对应的控制器,它应该是StoriesIndexController

Seanchai.StoriesIndexController = Ember.ArrayController.extend({    
  showNew: function() {
    this.set('isNewVisible', true);
  }
});

希望能帮助到你

于 2013-05-14T20:14:11.117 回答
0

我认为应该是:

Ember.ObjectController.extend({    

  showNew: function() {
    this.set('isNewVisible', true);
  }

});

代替:

Ember.ArrayController.extend({    

  showNew: function() {
    this.set('isNewVisible', true);
  }
});

本指南可能会有所帮助 - Ember.js - 模板

于 2013-05-14T19:54:53.383 回答