如何在相应路线外设置“新建”和“保存”按钮
How to have New and Save buttons outside the corresponding route
我有一个很容易用图片解释的挑战:应用程序控制器和文章的说明控制器
我想要的是,当我点击save时,在App.ArticlesNewController
中调用saveArticle
操作。
模板
<div class="buttonBlock">
{{#linkTo "articles.new" class="newButton"}}Nieuw{{/linkTo}}
{{#linkTo action="saveArticle" class="newButton storeButton"}}Opslaan{{/linkTo}} // generates an error: "This link-to is in an inactive loading state because at least one of its parameters presently has a null/undefined value, or the provided route name is invalid. "
</div>
路线
App.Router.map(function () {
this.resource('articles', {path: "/articles"}, function() {
this.resource('article', {path: '/:article_id'}); // works fine
this.route('new', {path: "/new"}); // displays also without errors
});
});
控制器
App.ApplicationController = Ember.ObjectController.extend({
actions: {
// got never called
saveArticle: function () {
console.log('ApplicationRoute saveArticle action');
this.controllerFor('articles.new').send('saveArticle')
}
}
});
路线
App.ArticlesNewRoute = Ember.Route.extend({
renderTemplate: function () {
console.log('ArticlesNewRoute try to render article with controller articles.new');
this.render('article', {
controller: 'articles.new'
});
},
model: function () {
console.log('ArticlesNewRoute returning model');
return this.store.createRecord('article');
},
actions: {
// this will never be called
saveArticle: function () {
console.log('ArticlesNewRoute saveArticle action');
this.currentModel.save();
}
}
});
请注意,目前我在App.ArticlesNewRoute
中有一篇新文章的操作,这显然不适用于this.controllerFor('articles.new').send('saveArticle')
调用。
saveArticle操作的真正问题是,它也从不在App.ApplicationController
中调用。
我还在模板中使用了{{controller}}
。我确信模板中的按钮是由App.ApplicationController
处理的,但调用从未被触发。
有什么想法吗?
您的代码中很少有问题
1) link-to
仅用于路由。在你的情况下,我建议如下。
<a class="newButton storeButton" {{action "saveArticle"}}>Opslaan</a>
2) 您不应该在控制器中使用this.controllerFor()
。据我记忆所及,它已被弃用。它目前仅在路线中可用。您应该使用needs
并尝试从中访问控制器。有关needs
的更多信息,请访问链接。
http://darthdeus.github.io/blog/2013/01/27/controllers-needs-explained/
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 多个单选按钮组相互干扰
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- Javascript按钮下拉列表
- jquery试图按名称获取按钮位置
- 漂亮照片图片库中的Facebook赞按钮
- 扩展移相器按钮类不工作
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 单击按钮以等待单击按钮
- 剑道UI内联编辑:如何在点击其他按钮时隐藏按钮
- 单击按钮后如何逐个调用分区,上一个分区将隐藏
- 如何从querySelectorAll中获取按钮类型
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 如何在单击“提交”按钮时为“新建”窗口编写JavaScript,用“确定”和“取消”显示注册信息
- 如何在相应路线外设置“新建”和“保存”按钮
- 删除单击“新建”按钮后添加的类
- 我可以让浏览器后退按钮转到编辑页面,而不是在创建时新建
- 新建<李>单击按钮时具有textarea的元素
- 从SharePoint 2013列表中删除编辑或新建项目按钮