Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在相应路线外设置“新建”和“保存”按钮_Javascript_Ember.js - Fatal编程技术网

Javascript 如何在相应路线外设置“新建”和“保存”按钮

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

我有一个很容易用图片解释的挑战:

我想要的是,当我点击save时,
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,他们只是来写一篇新文章的。昨晚我正要睡觉的时候,我想:也许我只是把按钮添加到文章视图中,我就完成了。我想这正是你想说的。谢谢