10

onClickビューイベントのある送信ボタンがあります。このイベントはフラグをチェックし、条件に応じてフォームの送信を許可します。submitコントローラーのアクションを呼び出したいです。これを行う最善の方法は何ですか?

4

4 に答える 4

23

ここでは、ビューでいくつかのロジックを実行し、その後コントローラーに委任する必要がある場合の albertjan の例に基づく別のソリューションを示します。これは私があなたの質問を理解した方法です:

HBS:

<script type="text/x-handlebars" data-template-name="index">
    <button {{action submit target="view"}} >Sumbit</button>
</script>

意見:

App.ThingView = Ember.View.extend({
    submit : function(){
            //do the view part of your logic
        var object = //do whatever you may need
        this.get("controller").send("submitInController", object); //you do not have to send object, if you do not need to
    }
});

コントローラ:

App.ThingController = Em.ObjectController.extend({
    submitInController: function(model) {
        // do the controller part of your logic
    }
});  

注:ビューからの呼び出しも、現在のルートにバブル アップします。つまり、これは基本的に、アクション ヘルパーを使用するときに ember が実行するコードと同じです。

于 2013-02-14T09:36:12.177 に答える
1

コントローラーでイベント全体を処理します。

HBS:

<script type="text/x-handlebars" data-template-name="index">
    <button {{action "submit"}}>Sumbit</button>
</script>

コントローラ:

App.ThingController = Em.ObjectController.extend({
    submit: function() {
        //handle things here!
        //change the state of your object here to reflect the changes that
        //the submit made so that the view shows these.
    }
});                   
于 2013-02-14T08:09:37.937 に答える
0

ビューがViewTargetActionSupport mixinを使用している場合、ビューからアクションをトリガーできます。次の例は、その使用法を示しています。

App.SomeController = Ember.Controller.extend({
    actions: {
        doSomething: function() {
            alert('Doing something!');
        }
    }
});

App.SomeView = Ember.View.extend(Ember.ViewTargetActionSupport, {
    someMethod: function() {
        this.triggerAction({action: 'doSomething'});
    }
});
于 2014-12-27T05:03:59.507 に答える