简体   繁体   English

灰烬-在路线上绑定自定义操作

[英]Ember - bind custom action on route

Is there a possibility to add custom action (basically any piece of JS code) to the routing event? 是否可以在路由事件中添加自定义操作(基本上是任何JS代码)?

There is an application which needs to alter some part of the page "outside" of the app. 有一个应用程序需要更改应用程序页面“外部”的某些部分。 Yes, that part of the page should be in the app, but it's not. 是的,页面的该部分应该在应用程序中,但不是。 So, I need to change the "outside" DOM a little on some routing events. 因此,我需要在某些路由事件上稍微更改“外部” DOM。 For example, when I'm at the index state, there is an empty element somewhere, when I move to another state, that element changes its content to "Back" and gets active. 例如,当我处于索引状态时,某处有一个空元素,当我移至另一状态时,该元素将其内容更改为“返回”并变为活动状态。 Also, I would like to bind the ember routing action to onClick event on that element. 另外,我想将余烬路由操作绑定到该元素上的onClick事件。

I tried to find such thing but there seems to be nothing. 我试图找到这样的东西,但似乎什么也没有。 I found this, but it does not solve my problem at all: How do I bind events directly to existing HTML, without using any kind of views, in Ember.js? 我发现了这一点,但根本没有解决我的问题: 如何在Ember.js中将事件直接绑定到现有HTML,而不使用任何类型的视图?

Thanks for your time. 谢谢你的时间。

It might help to know there is an enter event you could handle inside a route: 知道在路由内可以处理一个enter事件可能会有所帮助:

other: Ember.Route.extend({
    route: '/other',
    connectOutlets: function(router, context) { /* ...? */ }),
    enter: function(router) {
        // ...
    }
})

Not sure if there's a solution that better suits your needs. 不知道是否有一种更适合您需求的解决方案。

Update: If you use the action helper, the click event will trigger the action, as you desire. 更新:如果使用action帮助器,则单击事件将根据需要触发动作。 Example -- in the template: <a {{action gotoOtherState href="true"}}>Go to other state</a> , and in the route you were in (or an ancestor... the index route in this case): gotoOtherState: Ember.Route.transitionTo('path.to.otherState') . 示例-在模板中: <a {{action gotoOtherState href="true"}}>Go to other state</a> ,并在您所在的路线(或祖先...在这种情况下为索引路线) ): gotoOtherState: Ember.Route.transitionTo('path.to.otherState')

The href=true is an optional touch for <a> tags. href=true<a>标记的可选选项。 You can also define your own transitions: 您还可以定义自己的过渡:

gotoOtherState: function(router, event) {
    // Setup? Teardown?
    // ...
    router.transitionTo('path.to.otherState');
}

Note: if you pass a context in your action helper (eg {{action gotoOtherState this}} ), you can access it via event: router.transitionTo('path.to.otherState', event.context); 注意:如果在动作帮助器中传递上下文(例如{{action gotoOtherState this}} ),则可以通过以下事件访问它: router.transitionTo('path.to.otherState', event.context);

Update 2: enter is a private hook whereas activate is a new public hook (see http://emberjs.com/blog/2013/02/15/ember-1-0-rc/ ) 更新2: enter是一个私有钩子, activate是一个新的公共钩子(请参阅http://emberjs.com/blog/2013/02/15/ember-1-0-rc/

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

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