EmberJS 操作 - 当包装在“actions”中时从另一个操作调用一个操作

2024-01-07

当包裹在一个动作中时,如何从另一个动作中调用一个动作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对象似乎不再是控制器。

我该怎么做呢?


您可以使用send(actionName, arguments) method.

App.IndexController = Ember.ArrayController.extend({
    actions: {
        actionFoo: function() {
            alert('foo');
            this.send('actionBar');
        },
        actionBar: function() {
            alert('bar');
        }
    }
});

这是此示例的 jsfiddlehttp://jsfiddle.net/marciojunior/pxz4y/ http://jsfiddle.net/marciojunior/pxz4y/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

EmberJS 操作 - 当包装在“actions”中时从另一个操作调用一个操作 的相关文章

随机推荐