bgu*_*uiz 46 javascript publish-subscribe ember.js ember-controllers
当actions在EmberJS控制器中包含时,如何从另一个动作调用一个动作?
使用现已弃用的方法定义操作的原始代码:
//app.js
App.IndexController = Ember.ArrayController.extend({
    // properties
    /* ... */
    // actions
    actionFoo: function() {
        /* ... */
        this.actionBar();
    },
    actionBar: function() {
        /* ... */
    }
});
//app.html
<div class="foo" {{action actionFoo this}}>
<div class="bar" {{action actionBar this}}>
但是,使用EmberJS 1.0.0,我们会收到弃用警告,说必须将操作放在控制器内的操作对象中,而不是直接放在控制器中,如上所述.
根据建议更新代码:
//app.js
App.IndexController = Ember.ArrayController.extend({
    // properties
    /* ... */
    // actions
    actions: {
        actionFoo: function() {
            /* ... */
            this.actionBar(); //this.actionBar is undefined
            // this.actions.actionBar(); //this.actions is undefined
        },
        actionBar: function() {
            /* ... */
        }
    }
});
//app.html
<div class="foo" {{action actionFoo this}}>
<div class="bar" {{action actionBar this}}>
但是,我发现动作中定义的一个函数不可能调用另一个函数,因为该this对象似乎不再是控制器.
我该怎么做呢?
Mar*_*ior 101
您可以使用该send(actionName, arguments)方法.
App.IndexController = Ember.ArrayController.extend({
    actions: {
        actionFoo: function() {
            alert('foo');
            this.send('actionBar');
        },
        actionBar: function() {
            alert('bar');
        }
    }
});
这是一个jsfiddle这个样本http://jsfiddle.net/marciojunior/pxz4y/