Hi.
When we change page in Discourse, these are the apparent steps:
We click on the link
The current content disappears and the loader appears
The URL is updated and the new content appears
This is slightly different when we go from a page to the home, as the URL is changed to the home URL when the current content disappears, and not when the new content appears.
My goal is to have a script that is executed as soon as we click any page link on Discourse. api.onPageChange won’t fit in this case since the code is executed quite at the same time the new page content is loaded.
Is there a Discourse method for that? I look at the event triggers but didn’t find any one corresponding to what I need.
There’s currently no method in the Plugin-API that will allow you to fire a script before a page transition because this has not come up before as far as I recall.
That said, you can leverage on the willTransition() action in the application route
You would use something that like this in your theme / component
// this fires after the transition
api.onPageChange((url, title) => {
console.log("after transition");
});
// this fires right before the transition
api.modifyClass("route:application", {
pluginId: "some-name",
actions: {
willTransition() {
// run core code first
this._super(...arguments);
// then do some work
console.log("before transition");
// you can also do something like this to see what data you have
// to work with like _router
console.log(this)
}
}
});
putting actions in a hash is an old syntax which we no longer use in Discourse. Theoretically it should work, but the interop with the new syntax might not be perfect. The modern way would be
standard modifyClass disclaimer applies: this is risky and could break at any time
I don’t think willTransition is guaranteed to fire on the application route in all situations. (e.g. if a child route has a willTransition action, it won’t necessarily bubble up)
I think you’ll struggle to do this perfectly. Ember expects to be in full control of rendering, so trying to hook into arbitrary points will be hard, and may cause issues (depending what you’re trying to do).
Obviously I don’t know what your end goal is… so maybe the pain is justified. In which case: feel free to ignore me
(of course, if you’d like to open a Dev topic about the problem you’re trying to solve, I’d happily take a look)
I’m working with a third party ad provider and they have an ad page reset function in javascript that needs to be run before the new page’s ads are triggered.
any improvement is welcome and I’ve already realised this on the Topic List with Johani’s code. (in an open PR).
But this left the Topic … I will try your suggestion next and adopt it in both instances if that improves things further.
Ah I see, so you’re not modifying the DOM directly
In that case I hope routeWillChange will work - it should certainly fire before the next route is rendered.
Caveat is that… in the case of things like redirects, it may fire twice. Or may fire, and then the transition ends up being aborted. But maybe that’s fine in this case.