{"action":"created","issue":{"url":"https://api.github.com/repos/lhorie/mithril.js/issues/1095","repository_url":"https://api.github.com/repos/lhorie/mithril.js","labels_url":"https://api.github.com/repos/lhorie/mithril.js/issues/1095/labels{/name}","comments_url":"https://api.github.com/repos/lhorie/mithril.js/issues/1095/comments","events_url":"https://api.github.com/repos/lhorie/mithril.js/issues/1095/events","html_url":"https://github.com/lhorie/mithril.js/issues/1095","id":158532312,"number":1095,"title":"Add hook to router","user":{"login":"s3ththompson","id":970121,"avatar_url":"https://avatars.githubusercontent.com/u/970121?v=3","gravatar_id":"","url":"https://api.github.com/users/s3ththompson","html_url":"https://github.com/s3ththompson","followers_url":"https://api.github.com/users/s3ththompson/followers","following_url":"https://api.github.com/users/s3ththompson/following{/other_user}","gists_url":"https://api.github.com/users/s3ththompson/gists{/gist_id}","starred_url":"https://api.github.com/users/s3ththompson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/s3ththompson/subscriptions","organizations_url":"https://api.github.com/users/s3ththompson/orgs","repos_url":"https://api.github.com/users/s3ththompson/repos","events_url":"https://api.github.com/users/s3ththompson/events{/privacy}","received_events_url":"https://api.github.com/users/s3ththompson/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/lhorie/mithril.js/labels/discussion","name":"discussion","color":"d4c5f9"},{"url":"https://api.github.com/repos/lhorie/mithril.js/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/lhorie/mithril.js/labels/rewrite","name":"rewrite","color":"0e8a16"}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/lhorie/mithril.js/milestones/1","html_url":"https://github.com/lhorie/mithril.js/milestones/Rewrite","labels_url":"https://api.github.com/repos/lhorie/mithril.js/milestones/1/labels","id":1835772,"number":1,"title":"Rewrite","description":"Issues and pull requests geared towards completing the rewrite.","creator":{"login":"isiahmeadows","id":4483844,"avatar_url":"https://avatars.githubusercontent.com/u/4483844?v=3","gravatar_id":"","url":"https://api.github.com/users/isiahmeadows","html_url":"https://github.com/isiahmeadows","followers_url":"https://api.github.com/users/isiahmeadows/followers","following_url":"https://api.github.com/users/isiahmeadows/following{/other_user}","gists_url":"https://api.github.com/users/isiahmeadows/gists{/gist_id}","starred_url":"https://api.github.com/users/isiahmeadows/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/isiahmeadows/subscriptions","organizations_url":"https://api.github.com/users/isiahmeadows/orgs","repos_url":"https://api.github.com/users/isiahmeadows/repos","events_url":"https://api.github.com/users/isiahmeadows/events{/privacy}","received_events_url":"https://api.github.com/users/isiahmeadows/received_events","type":"User","site_admin":false},"open_issues":9,"closed_issues":30,"state":"open","created_at":"2016-06-18T08:01:00Z","updated_at":"2016-08-15T15:11:14Z","due_on":null,"closed_at":null},"comments":51,"created_at":"2016-06-05T01:20:44Z","updated_at":"2016-08-16T20:20:41Z","closed_at":"2016-08-15T15:00:55Z","body":"A feature request for the Mithril rewrite:\r\n\r\nI love the simplicity of the current router, but I would like to expose a hook that runs after a route has been resolved but before the associated component has been rendered. The feature would be useful for implementing a few common patterns:\r\n- `/` routes to a landing page if the user is logged out, but `/dashboard` if the user is logged in\r\n- `/dashboard` routes to a dashboard if user is logged in, but `/login` if user is logged out\r\n\r\nThere is lots of precedent for this type of hook in other routers. See, for example, [`onenter`](https://github.com/reactjs/react-router/blob/master/docs/API.md#onenternextstate-replace-callback) in react-router and the ability to extend [`router.execute`](http://backbonejs.org/#Router-execute) in Backbone.\r\n\r\n##### Current solution in Mithril\r\n\r\n```js\r\nvar Dashboard = {\r\n  oninit: function(vnode) {\r\n      if (!Auth.loggedIn()) {\r\n        m.route.setPath(\"/login\");\r\n      }\r\n  }\r\n// *** view code ***\r\n}\r\n```\r\n\r\nUnfortunately, since `setPath` asynchronously triggers a reroute *only after the `window.onpopstate` event fires*, the dashboard component above renders to the screen for a split second before the login page is loaded.\r\n\r\n##### Propsed API Options\r\n\r\nAdd an `onroute` hook to components that runs before the component is rendered and has the option to redirect to another route without rendering the component?\r\n\r\nAlternatively, if `oninit` returns false, prevent rendering of the component?"},"comment":{"url":"https://api.github.com/repos/lhorie/mithril.js/issues/comments/240225332","html_url":"https://github.com/lhorie/mithril.js/issues/1095#issuecomment-240225332","issue_url":"https://api.github.com/repos/lhorie/mithril.js/issues/1095","id":240225332,"user":{"login":"pygy","id":54515,"avatar_url":"https://avatars.githubusercontent.com/u/54515?v=3","gravatar_id":"","url":"https://api.github.com/users/pygy","html_url":"https://github.com/pygy","followers_url":"https://api.github.com/users/pygy/followers","following_url":"https://api.github.com/users/pygy/following{/other_user}","gists_url":"https://api.github.com/users/pygy/gists{/gist_id}","starred_url":"https://api.github.com/users/pygy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pygy/subscriptions","organizations_url":"https://api.github.com/users/pygy/orgs","repos_url":"https://api.github.com/users/pygy/repos","events_url":"https://api.github.com/users/pygy/events{/privacy}","received_events_url":"https://api.github.com/users/pygy/received_events","type":"User","site_admin":false},"created_at":"2016-08-16T20:20:41Z","updated_at":"2016-08-16T20:20:41Z","body":"I missed @barneycarroll 's two posts before posting the last message. If we are to duplicate the component instantiation and lifecycle in the router, then it would make sense for the state to be preserved as long as the same component/RouteResolver is picked after matching a path to a route, even if different routes map to the same object."}}
{"actor":{"display_login":"pygy"}}