{"action":"created","issue":{"url":"https://api.github.com/repos/lhorie/mithril.js/issues/1180","repository_url":"https://api.github.com/repos/lhorie/mithril.js","labels_url":"https://api.github.com/repos/lhorie/mithril.js/issues/1180/labels{/name}","comments_url":"https://api.github.com/repos/lhorie/mithril.js/issues/1180/comments","events_url":"https://api.github.com/repos/lhorie/mithril.js/issues/1180/events","html_url":"https://github.com/lhorie/mithril.js/issues/1180","id":168274967,"number":1180,"title":"[rewrite] oninit not called when setting the same route","user":{"login":"veloce","id":423393,"avatar_url":"https://avatars.githubusercontent.com/u/423393?v=3","gravatar_id":"","url":"https://api.github.com/users/veloce","html_url":"https://github.com/veloce","followers_url":"https://api.github.com/users/veloce/followers","following_url":"https://api.github.com/users/veloce/following{/other_user}","gists_url":"https://api.github.com/users/veloce/gists{/gist_id}","starred_url":"https://api.github.com/users/veloce/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/veloce/subscriptions","organizations_url":"https://api.github.com/users/veloce/orgs","repos_url":"https://api.github.com/users/veloce/repos","events_url":"https://api.github.com/users/veloce/events{/privacy}","received_events_url":"https://api.github.com/users/veloce/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-07-29T08:25:59Z","updated_at":"2016-07-29T09:42:17Z","closed_at":null,"body":"### Description:\r\n\r\nThis is a follow-up of #1177 and #1178, but this time I clearly identified the problem. \r\n\r\nThe problem arises when calling `m.route.set` on the same route we are currently in. Same route is either `/route` or `route/:param` even if the route arg changes.\r\nIn that case `oninit` is not called. Since it is used to load data, new data will never load, thus the screen never updates.\r\n\r\n### Steps to Reproduce:\r\n\r\nhttps://jsbin.com/tiwiyuqifo/edit?js,console,output\r\n\r\n```\r\nm.route(document.body, '/', {\r\n  '/': {\r\n      view : () => m(\"div\", \"Home\")\r\n  },\r\n  '/game/:id' : {\r\n    oninit( vnode ){\r\n      console.log(`oninit ${vnode.attrs.id}`);\r\n      this.id = vnode.attrs.id;\r\n    },\r\n    view() {\r\n      return m( 'div', 'game ' + this.id);\r\n    }\r\n  }\r\n});\r\n\r\nsetTimeout(() => m.route.set('/game/1234'), 2000);\r\nsetTimeout(() => m.route.set('/game/5678'), 5000);\r\n```\r\n\r\n### Expected:\r\n\r\nWe should see 'game 1234' then 'game 5678'.\r\n\r\n### Actual:\r\n\r\nWe only see 'game 1234'.\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/lhorie/mithril.js/issues/comments/236138797","html_url":"https://github.com/lhorie/mithril.js/issues/1180#issuecomment-236138797","issue_url":"https://api.github.com/repos/lhorie/mithril.js/issues/1180","id":236138797,"user":{"login":"veloce","id":423393,"avatar_url":"https://avatars.githubusercontent.com/u/423393?v=3","gravatar_id":"","url":"https://api.github.com/users/veloce","html_url":"https://github.com/veloce","followers_url":"https://api.github.com/users/veloce/followers","following_url":"https://api.github.com/users/veloce/following{/other_user}","gists_url":"https://api.github.com/users/veloce/gists{/gist_id}","starred_url":"https://api.github.com/users/veloce/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/veloce/subscriptions","organizations_url":"https://api.github.com/users/veloce/orgs","repos_url":"https://api.github.com/users/veloce/repos","events_url":"https://api.github.com/users/veloce/events{/privacy}","received_events_url":"https://api.github.com/users/veloce/received_events","type":"User","site_admin":false},"created_at":"2016-07-29T09:42:17Z","updated_at":"2016-07-29T09:42:17Z","body":"Indeed, if I want to reroute the same route with a different id I could do something like:\r\n\r\n```js\r\n'/game/:id' : {\r\n    oninit( vnode ){\r\n      console.log(`oninit ${vnode.attrs.id}`);\r\n      this.id = vnode.attrs.id;\r\n    },\r\n   onbeforeupdate(vnode, old) {\r\n      // when attrs are different it means we changed route\r\n      if (vnode.attrs.id !== old.attrs.id) {\r\n         this.id = vnode.attrs.id;\r\n      }\r\n   },\r\n    view() {\r\n      return m( 'div', 'game ' + this.id);\r\n    }\r\n  }\r\n```\r\n\r\nBut something tells me, that it doesn't \"feel right\" to use `onbeforeupdate` to do this. It's like using a rather obscure lifecycle method (that is called on each redraw) to do something trivial, not related to redraw, but routing. But maybe I'm wrong.\r\n\r\nMoreover the `onbeforeupdate` trick doesn't address the problem when I want to reload the same page (see #1177). I have a use case for that in my app. On lichess tv, I'd receive a socket event telling me that a new game is to be shown, event that triggers a `m.route.set('/tv')` to reload the screen. As a workaround I could push the current game id to the tv url but it would not feel right as well. After all, reloading the same screen should be a valid use case, right?\r\n\r\nI don't know the reason why now components are not rebuilt on route change, so I'm just throwing suggestions here. Maybe for a routing purpose we could have a new lifecycle method, like `onreenter` even though, when saying that, I feel that it would break the coherence of the whole lifecycle events we have now and that are related to vnodes, not routing.\r\n\r\nActually my first impression here is that rebuild root components on route change is the simplest way to address this problem, but again, I don't know the implication of this in the rest of mithril.\r\nSo at least, imo, if this is the desired behavior, there should be something in the doc that explains this, because you can't just reuse your v0 `controller` as `oninit` in v1.0. "}}
{"actor":{"display_login":"veloce"}}