我正在制作我的第一个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'
});
。/控制器/stories_controller.js:
Seanchai.StoriesController = Ember.ArrayController.extend({
showNew: function() {
this.set('isNewVisible', true);
}
});
。/模板/故事/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'
});
我显然错过了一些东西,但我不确定是什么
我猜你的showNew
事件没有在控制器上触发因为你有一个stories.index
模板所以你应该挂钩到相应的控制器应该是StoriesIndexController
:
Seanchai.StoriesIndexController = Ember.ArrayController.extend({
showNew: function() {
this.set('isNewVisible', true);
}
});
希望有所帮助
我认为应该是:
Ember.ObjectController.extend({
showNew: function() {
this.set('isNewVisible', true);
}
});
代替:
Ember.ArrayController.extend({
showNew: function() {
this.set('isNewVisible', true);
}
});
- Ember.js - Templates.