Javascript 如何在相应路线外设置“新建”和“保存”按钮
我有一个很容易用图片解释的挑战: 我想要的是,当我点击save时,Javascript 如何在相应路线外设置“新建”和“保存”按钮,javascript,ember.js,Javascript,Ember.js,我有一个很容易用图片解释的挑战: 我想要的是,当我点击save时,saveArticle操作在App.ArticlesNewController中调用 模板 <div class="buttonBlock"> {{#linkTo "articles.new" class="newButton"}}Nieuw{{/linkTo}} {{#linkTo action="saveArticle" class="newButton storeButton"}}Opslaan
saveArticle
操作在App.ArticlesNewController
中调用
模板
<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.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.ApplicationController = Ember.ObjectController.extend({
actions: {
// got never called
saveArticle: function () {
console.log('ApplicationRoute saveArticle action');
this.controllerFor('articles.new').send('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.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)
链接到
仅用于路线。在你的情况下,我建议如下
<a class="newButton storeButton" {{action "saveArticle"}}>Opslaan</a>
您是否打算将其他型号的产品放在销售点?IE,new和save最终也会被用来控制其他模型吗?@michaeljohnston,他们只是来写一篇新文章的。昨晚我正要睡觉的时候,我想:也许我只是把按钮添加到文章视图中,我就完成了。我想这正是你想说的。谢谢