简体   繁体   English

从组件触发路由动作

[英]Trigger route action from component

I have a component that I want to trigger a route level action on so I can transition to a different route.我有一个组件,我想在其上触发路由级别操作,以便我可以转换到不同的路由。

App = Ember.Application.create();

App.Router.map(function() {
});

App.IndexRoute = Ember.Route.extend({
  actions: {
    complete: function() {
      // This never happens :(
      console.log('Triggered complete!');
    }
  }
});

App.MyAreaComponent = Ember.Component.extend({
  actions: {
    clickMyButton: function() {
      console.log('Triggering complete action.');
      // Attempting to trigger App.IndexRoute.actions.complete here
      this.sendAction('complete');
    }
  }
});

What I am trying to accomplish is when MyAreaComponent's 'clickMyButton' action is triggered, it will trigger the IndexRoute's 'complete' action.我想要完成的是当 MyAreaComponent 的“clickMyButton”操作被触发时,它将触发 IndexRoute 的“完成”操作。

I have set up a jsbin to demonstrate my issue:我已经设置了一个 jsbin 来演示我的问题:

http://emberjs.jsbin.com/wivuyike/1/edit http://emberjs.jsbin.com/wivuyike/1/edit

According to the EmberJs documentation on action bubbling :根据EmberJs 关于动作冒泡的文档

If the controller does not implement a method with the same name as the action in its actions object, the action will be sent to the router, where the currently active leaf route will be given a chance to handle the action.如果 controller 没有在其操作 object 中实现与操作同名的方法,操作将被发送到路由器,当前活动的叶子路由将有机会处理操作。

So with this in mind I would expect the component to say "Let's check my controller and see if it has an action called 'complete'. No? Ok, let's check my route (IndexRoute) to see if it has an action called 'complete'. Yes? Ok, trigger it!"因此,考虑到这一点,我希望组件会说“让我们检查我的 controller,看看它是否有一个名为‘完成’的动作。没有?好的,让我们检查我的路线 (IndexRoute),看看它是否有一个名为‘完成’的动作'. 是吗?好的,触发它!

The only thing I can think of is that because of the way the component is set up, IndexRoute isn't set as the route of the component, so the action bubbling just stops at the controller.我唯一能想到的是,由于组件的设置方式,没有将IndexRoute设置为组件的路由,所以冒泡的动作只是停在了controller。

I'm unsure where to go from here.我不确定从这里到哪里去 go。 Do I need to do something special to make my component aware of my IndexRoute?我是否需要做一些特别的事情来让我的组件知道我的 IndexRoute?

Here is your updated sample - http://emberjs.jsbin.com/wivuyike/3/edit 这是您更新的样本 - http://emberjs.jsbin.com/wivuyike/3/edit

So from the component your action need to bubble herself up, this can be done by 因此,从组件中你需要让自己冒出来,这可以通过以下方式完成

this.sendAction('clickMyButton');

and then when you use your component, assign route action which needs to be triggered to your component action as below 然后在使用组件时,将需要触发的路径操作分配给组件操作,如下所示

{{my-area clickMyButton='complete'}}

in the template next to the route:在路线旁边的模板中:

{{component-name refresh=(route-action "refresh")}}

then in component you can call it:然后在组件中你可以调用它:

this.get('refresh')();

An action can be called from a component template by creating another action in the component.通过在组件中创建另一个动作,可以从组件模板中调用一个动作。

So it saves me time today.所以今天它节省了我的时间。

If you are doing nothing else in the action, except for sending an action to the route, you can try doing this directly from the template: 如果您在操作中没有执行任何其他操作,除了向路径发送操作之外,您可以尝试直接从模板执行此操作:

<button {{action "complete" target="route"}}>Complete</button>

(Untested code, make sure you test first!) (未经测试的代码,请确保先测试!)

... otherwise it looks like you should do: ...否则看起来你应该这样做:

this.sendAction('action', 'complete');

instead of: 代替:

this.sendAction('complete');

So it looks like you were just missing that first parameter in the code posted in your question. 所以看起来你只是错过了问题中发布的代码中的第一个参数。 Reference 参考

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

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