【发布时间】:2013-05-14 18:49:06
【问题描述】:
我正在开发我的第一个 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'
});
我显然遗漏了一些东西,但我不确定是什么。
【问题讨论】:
标签: ember.js