简体   繁体   English

使Ember路由器从点击事件过渡到路由

[英]Make Ember router transition to route from a click event

I got a Leaflet map rendered as a view with controller in application template like {{view "map" locations }} . 我在应用程序模板({{view "map" locations }}中将控制器地图呈现为带有控制器的{{view "map" locations }} locations is an array of location models. 位置是位置模型的数组。

In index route the locations data is loaded and set on application controller to be passed on to the map view. 索引路由中位置数据已加载并在应用程序控制器上设置,然后传递到地图视图。 This happens that way because other routes can set another content for locations and so change the markers on the map. 发生这种情况是因为其他路线可以为位置设置其他内容,并因此更改地图上的标记。

In the map view the Leaflet map is set up and the markers are created from locations . 地图视图中,设置了传单地图,并从位置创建了标记。 I want to be each marker to be clickable, so the app transitions to the route where the “clicked” location is described in detail. 我希望每个标记都可以单击,因此应用程序过渡到详细描述“被单击”位置的路线。 Therefor I have to bind a click event handler right after creation when I have the location model at hand like 因此,当我手边的位置模型像

mapMarker.on('click', function(ev) {
  // Do transition to 'location' with model location
});

I'd love to keep the routing stuff in the router / routes . 我希望将路由内容保留在路由器 / 路由中 In a template I'd use a link-to helper, but how to a transition from a click event defined within a view? 在模板中,我将使用链接帮助器,但是如何从视图中定义的click事件过渡? I tried to use sendAction but it seems this doesn't work here because of the view being rendered in its on scope so the action doesn't bubble up to application route . 我尝试使用sendAction,但是似乎在这里不起作用,因为视图是在其作用域内呈现的,因此该动作不会冒泡到应用程序路由 Any ideas? 有任何想法吗?

For the click event to bubble up to the routes you have to send it to the current controller. 为了使单击事件冒泡到达路由,您必须将其发送到当前控制器。

this.get('controller').send('someEvent')

Doing just send() makes the action bubble up only to parent views. 仅执行send()会使操作仅出现在父视图中。

Now, if you were to use the action helper in your template, in that case it goes directly to the controller. 现在,如果您要在模板中使用action帮助器,在这种情况下,它将直接进入控制器。 And if the controller doesn't implement the action, it will bubble up to the route. 如果控制器不执行该操作,它将冒泡到达该路线。

See http://emberjs.com/guides/templates/actions/#toc_action-bubbling 请参阅http://emberjs.com/guides/templates/actions/#toc_action-bubbling

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

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