most reusable and least messy way to link query parameters to refs
So typical search design - you change a certain search term, whether that be a boolean, dropdown, search query etc - I'd ideally like that to be binded with the query parameters in your URL so that you can copy and paste it elsewhere for someone else
I understand the basic way to do this
Have the ref default to the route.query.my_key - then use a watch on the reference and when it updates push to the router.
This is good sure - however I have several inputs and having a watch for every single variable will be messy and ugly - especially since they're doing the same thing.
I'm asking on whats the best way to make this a lot of cleaner and more reusable for other pages with search filters.
I was hoping to create an easy to use composable but wouldnt know where to start with watches and such as its not delved in territory for me so Im asking for pointers and or ideas :)
46 Replies
You could merge all the input refs into a single set/get computed state. The get reads and returns the url’s query params, and the set updates the url
so keep the input refs, have a computed state in which the getter returns a string of the query params and the setter sets it?
how would i go about doing the watch for the input still - as id need to check for updates
might b e reading this wrong
You wouldn’t need a watcher in this case. The get watches it’s dependencies ie; the route
No, the getter would return an object where each field maps to each input
okay yeah thought so, ill give it a shot and come back if i get lost
i assume you mean
<input v-model="computed.my_key" />
then the setter essentially updates the url with the updated values ( goes through the object )
Exactly
cool got it, will experiment and give it a shot :) appreciate it boss
Awesome. You’re welcome
with the setter, would I have to update the values or can I have it just push the router? In my getter im returning an object of refs ( i assume thats the right course ) and then it should just handle it like that, and the setter is only used for updating the route, it doesnt need to update the object full of refs
^ this is my iteration right now - may be missing something as I havent tested it yet
i assume i dont need the refs as the computed is a ref in itself?
reading it back seems super redundent to do refs inside that object
seems like the setter isn't getting hit with the v-models ( also removed all the refs - redundent )
You don’t need to wrap the values in a ref
yeah got rid of them as realised computed is a ref 😭
only issue right now is set is never getting ran
also this seems to make my ui elements sluggish? not sure why that'd be
seems like it doesn't like the whole computed.field sort of deal, it doesn't register updates here and there, but sometimes it does
hmm. that shouldn't affect the ux
yeah it doesnt seem to be the UX more rather it seems to not be updating it all the time
hmm. lemme check and see why
here's me spam clicking the toggle
you can see it registers the update sometimes but doesnt others
altho it seems like a ux thing because in some positions it just doesnt work, this is so lost on me haha
possible to create reproduction so I check it out?
so it only update when my mouse it out of view of the UX is what i've figured out - and how do you mean sorry
here's my computed - and as for my ui im using nuxt ui components
<USelectMenu v-model="fields.alert_severity" :options="['Low', 'Medium', 'High']" multiple class="w-36" />
I might be wrong but I'm thinking the component might be the cause
yeah seems like it
only updates when i move my mouse away
just tried with this sample and here's how it works on my end. no issues
like a copy of it on Stackblitz
issue is this is a massive repo of work and also it's my work work so wouldnt be allowed, as much as Id want to :(
yeah this is bizarre, i meant my set isn't even running
doesn't have to be exact. just a piece of it
only pieces ive got are the ones above, the computed, then any component using said computed via v-model using nuxt ui components
I guess you were right. It's doesn't work exactly as expected. Not sure why though
try this alternative
@Gobbo
will give it a shot :)
alright
works like a charm
only issue is with arrays, so with dropdowns if you only have one option selected, the query params seems like a string rather than array as its only showing one option in the url - thus breaking the dropdown entirely ( errors )
not sure if theres a way to be more verbose about arrays in the query params like introducing [] in front, do you know of anyway to tell nuxt that or?
nuxt router handles that very well
?per_page=20&alert_severity=Low&alert_severity=Medium&alert_severity=High&alert_state=Triggered&include_dismissed=false
per_page=20&alert_severity=High&alert_state=Triggered&include_dismissed=0
issue is here - it doesn't like single values, maybe because it thinks its a string? ill see if i can be verbose about the type
doesnt seem to like it
oh I get what you mean. you'd then have to transform the value
yeah going to try just manually transform it
seems just wrapping it in [] did the trick :)
alert_severity: [route.query.alert_severity] || ['Low', 'Medium', 'High'],
not sure i its bad practice or not, but it works
appreciate all the help boss :)
not sure if theres a way to make this reusable in some form of composable but ill mess around
great. I don't think it's bad practice, but you could as the nuxt team in the general channel
nevermind its bad practice, as it wraps the initial array ( query params with multiple values ) in another array :) ill mess around
thanks for all the help
anytime
you could try something like this if you want
could work, would also work if i transformed it back into an array too
:)
good idea
yeah. the transformation is happening in both directions. ie; string-to-array in the reactive state and array-to-string in the watcher
yeah thats what i opted for, read ur code wrong for a sec but got it
no probs. great u figured it out
appreciate it :) not sure if i can turn this into a composable without h eaving typescript work - would need some hard dynamic typing and dynamic watchers etc
for another time
this is short enough for me
cool. I'll let you know if I come up with something
no promises