-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reloading current route #126
Comments
What's your use case? In general, anything that should cause your states to reload should be represented in the route. I'd need to know more about your specific use case to give better advice. What is changing that makes you want to re-run all the |
The idea is to enter the route and reload the specified template even though the user is already in that route. |
Right, I got that, but why? What is happening in your application that makes that a thing you want to do? |
Allow users to reload the template by navigating to the same url address. In Angular we can either set the route to not be reused or configure a behaviour to onSameUrlNavigation. |
Why do you want users to be able to reload the page? What is going on that makes a reload a good idea? Is it something in a |
I want them to restart the component without having a 'restart button'. Not sure this is achievable by configuring the states or resolves. |
Why do you want them to restart the component? What is your app doing? Why does the component need to be restarted? |
This is inevitably tied to your specific application needs - I would really recommend joining the chat and chatting with other ASR users about how you should accomplish what you need |
I'm turning around on this idea – I think there are good use cases for reloading some of the current states, and while talking to @saibotsivad the other day, I thought of a way to implement it. State changes are driven by the browser's location right now, and the I don't want to mess with that paradigm, so I wouldn't tie this reload-without-a-location-change functionality to But maybe there should be a new Thoughts? |
As far as naming goes, there is precedence for |
Suppose that I'm at a state like I think I would like to be able to re-run the resolve etc. functions of However, if it re-ran all the resolves etc., that would still be okay, the asr user (me!) would be able to implement some server or browser side caching, to make sure the reloading didn't take too long. |
I currently use a mediator for this concept. So in this example, the |
That's essentially what I'm starting to work toward as well. Getting it started has felt kind of clunky, but it's pretty clear that after I make a handful, some obvious tooling will present itself that will make it cleaner and easier. |
Unfortunately, newer versions of mannish don't support the concept of multiple providers handling the same named function. Thus, I'm still on an older version. |
I started developing my tooling for this using this emitter. There's a little work with making sure to unsubscribe the emitter when a component is destroyed, etc., but it's been pretty good so far. 🤞 |
@saibotsivad that's what I'm aiming for – presumably, that state depends on a |
I'm feeling pretty good about this feature so far. We can keep workshopping the name. In the meantime, if anyone wanted to write a unit test or two, I'd much appreciate it. A test could start with a copy/paste from https://github.com/TehShrike/abstract-state-router/blob/master/test/test.js changed to assert:
You can use whatever dummy method name you like for now in the tests. |
I am in need of this functionality. As an example of what I am trying to do, I have route TBH, I would be happy if I could just do something like |
@sw-clough that's a good use case to note – where you are actually doing a state change, but you need a parent of the changed state to reload as well. It would be good to support that too. In your case, is Can you give your concrete example instead of using I don't want to do a quick/easy implementation – I'm pretty sure this functionality can be implemented in a way that fits in nice with the current ASR paradigms, where we just say "this is the browser navigation that should happen, and also for the purpose of state changes, these parameters should be treated as if they had changed as well". |
@sw-clough This might not work for you, but I'll show you one of the ways that I manage updates. I leave it here in hopes that it helps you, or someone else. In my application, I create a global emitter: // emitter.js
const createEmitter = require('better-emitter')
const emitter = createEmitter()
module.exports = emitter Then in the child view, e.g. // foo-view.js
const emitter = require('./emitter.js')
//...
asr.addState({
name: 'foo.view',
activate: ({ domApi }) => {
domApi.on('change', title => {
emitter.emit('change-title', title)
})
}
}) And in any view that cares about that property, I watch for those changes (be careful to not leave event emitter listeners lying around): // foo.js
const emitter = require('./emitter.js')
//...
asr.addState({
name: 'foo',
activate: ({ domApi }) => {
const unsubscribe = emitter.on('change-title', title => {
domApi.set({ title })
})
domApi.on('destroy', () => {
if (unsubscribe) {
unsubscribe()
}
})
}
}) In practice, I've made some wrapper functions so I can register a component for changes, and handle the subscribe/unsubscribe functionality without a lot of repeated code. There may be easier ways to manage state inside your framework (for example "stores" in Svelte3), but something along these lines: // state.js
const emitter = require('./emitter.js')
module.exports = {
subscribe: (domApi, key, cb) => {
const unsubscribe = emitter.on(key, props => {
cb(props)
})
domApi.on('destroy', () => {
if (unsubscribe) {
unsubscribe()
}
})
}
} And then in the watching state: // foo.js
const { subscribe } = require('./state.js')
//...
asr.addState({
name: 'foo',
activate: ({ domApi }) => {
subscribe(domApi, 'change-title', title => domApi.set({ title }))
}
}) |
A Here's an example use case for me: The user can perform certain operations on the results that might change which results should be shown. Also the user could leave the results open for days and decide to reperform the search when they return(data could have changed on the server). Currently to do this, they have to either go to a different state and return, or refresh the browser(entire web app), neither of which is a great solution. My current workaround is to make a parameter be a uuid and generate a new one on each |
That argument for baking it into You would only want the results state to be reloaded in that case, right? You wouldn't want every parent state to reload and re-render? I'm imagining that rather than a boolean, You could pass in the names of all of the parameters that are relevant to the search results, and the search results would always be refreshed even if the values hadn't changed. Alternately, if you only wanted the This might be easier/nicer in some circumstances, but I don't feel as good about it, because it kind of steps around the "the parameters drive the states" model. |
Is there any way to reload the current route? I know I can just not use event.preventDefault() but that reloads the app as a whole.
The text was updated successfully, but these errors were encountered: