简体   繁体   English

如何在进行转换时从Ember路径调用控制器操作?

[英]How do I call a controller action from an Ember route while doing a transition?

My objective is to display a fancy "loading..." graphic on my page while Ember fetches model data through the Ember route. 我的目标是在我的页面上显示一个花哨的“loading ...”图形,而Ember通过Ember路线获取模型数据。

This led me to http://emberjs.com/guides/routing/loading-and-error-substates/ . 这导致我http://emberjs.com/guides/routing/loading-and-error-substates/ That inspired me to create an action on my page's controller which would show the "loading" overlay window in the DOM. 这激发了我在我的页面控制器上创建一个动作,它将在DOM中显示“加载”覆盖窗口。 For example, here's my controller: 例如,这是我的控制器:

controllers/users.js: 控制器/ users.js:

export default Ember.ArrayController.extend({
    ...
    actions: {
        displayLoading: function() {
            // Show the DOM element that says "Loading..."
        },
        ...
    }
});

I'd like to call that while my data is loading, so I then define a route as follows: 我想在我的数据加载时调用它,所以我然后定义一个路由如下:

routes/users.js: 路线/ users.js:

export default Ember.Route.extend({
    model: function( params ) {
        return this.store.find('user', params );
    },

    actions: {
        loading: function(transition, originRoute) {
            transition.send('displayLoading');
        }
    }
});

But when I do this, I get this error: 但是,当我这样做时,我收到此错误:

Uncaught Error: Nothing handled the action 'displayLoading'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.

So my question is where can I define this action so that my loading method will be able to call it? 所以我的问题是我在哪里可以定义这个动作,以便我的loading方法能够调用它?

Note that trying this.send('displayLoading') gave me this error: 请注意,尝试this.send('displayLoading')给了我这个错误:

Can't trigger action 'displayLoading' because your app hasn't finished transitioning into its first route. To trigger an action on destination routes during a transition, you can call .send() on the Transition object passed to the model/beforeModel/afterModel hooks. .

Update: I am able to catch this action on the route itself, but then I still can't call the action on my controller. 更新:我能够在路线本身上捕捉到这个动作,但是我仍然无法在我的控制器上调用该动作。

Update #2: Thanks to @kingpin2k's answer, I've resolved this. 更新#2:感谢@ kingpin2k的回答,我已经解决了这个问题。 For those interested, here is a full solution: 对于那些感兴趣的人,这是一个完整的解决方

controllers/users.js: 控制器/ users.js:

export default Ember.ArrayController.extend( {
    actions: {
        showLoading: function() {
            this.set('isLoading', true);
        },

        hideLoading: function() {
            this.set('isLoading', false);
        },
    }
});

routers/users.js: 路由器/ users.js:

export default Ember.Route.extend({

    model: function( params ) {
        return this.store.find('user', params );
    },

    actions: {
        loading: function() {
            this.controllerFor('users').send('showLoading');
        },
        didTransition: function() {
            this.controllerFor('users').send('hideLoading');
        }
    }

});

A key insight was that I can set an isLoading property on my controller which determines whether my modal "Loading..." window is showing in the Handlebars template. 一个关键的见解是我可以在我的控制器上设置一个isLoading属性,它确定我的模态“正在加载...”窗口是否显示在Handlebars模板中。

use controllerFor , http://emberjs.com/api/classes/Ember.Route.html#method_controllerFor 使用controllerForhttp://emberjs.com/api/classes/Ember.Route.html#method_controllerFor

    loading: function(transition, originRoute) {
        var controller = this.controllerFor('foo');
        controller.send('displayLoading');
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM