Iron Router hook on a route before actually changing the template

▼魔方 西西 提交于 2019-12-24 08:31:04

问题


i am trying to show the user a payment popup as soon as he clicks on a payed object. But after he pays he should directly enter the content he clicked on.

Therefore i think its a good solution to solve this with the router, because i want every link on the page that redirects to this content to show this popup.

My problem is i want to show the popup before redirecting the user. So i tryed the onBeforeAction hook and stuff but everything working with the iron router seems to only hook in after the URL of the browser changed and the current template was unloaded.

Do you have an idea how to get this kind of behavior?

Cheers


回答1:


Based on this answer, here is how you can hook the router using Router.onStop():

// onStop hook is executed whenever we LEAVE a route
Router.onStop(function(){
  //check if the current route is the page from where you need to show your 
  //popup and show it based on, for instance, a session variable containing 
  //the previously clicked content id.

});



回答2:


It's a common use case that I don't think is directly achievable within the iron router framework at present (although I would be delighted to be corrected!). As you've discovered, onBeforeAction is run before the page has rendered but after the new route has been run, so the old page has already disappeared.

Effectively, you're looking to queue the running of a new route until a certain action has been completed. The use case for which I've experienced this requirement is page transitions, for which the best solution appears to be to do completely the opposite of what you propose: i.e. to add the logic to an event attached to the link, and only redirect to the new route once that logic has been satisfactorily completed (i.e. the popup has been closed in your case).

I agree that doing something in the router would be a sensible way to approach this, but I'm not sure it's possible in iron router as things stand. Note that this has already been raised though!




回答3:


Will this workshop? 'unload - runs just once when you leave the route for a new route.' From https://github.com/EventedMind/iron-router/blob/devel/DOCS.md#unload-hook



来源:https://stackoverflow.com/questions/25532018/iron-router-hook-on-a-route-before-actually-changing-the-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!