Super-select: update on dependent field #45
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #42
Co-dependent PRs:
This PR gives the ability for a super-select to update its options (by updating its searchUrl) based on some other field on whose value it depends on.
It introduces a
dependable_controller.js
, which allows for direct dispatching of events to adependent
field without the need for a wrapper controller to coordinate events.The new
outlets
feature of Stimulus 3.2 was considered, but it's the wrong pattern because it requires tight coupling of the dispatching and the dependent fields (ie. knowledge from the dispatcher of the inner-workings of the receiver controller). Since thedependable_controller
could be used to also update dependentturbo_frames
or other arbitrary fields, theoutlets
pattern no longer fits the bill.Example:
Let's hook up a
:region
super-select field to update itself with new options when the:country
field changes (in this case, it's a super-select also, but doesn't have to be):Tasks:
Updating the
searchUrl
from a pattern and mappingsFor the
searchUrl
to be able to update itself in a number of ways, I'd like to introduce the ability to definerequired
andoptional
params for thesearchUrl
.Given a url pattern like
/account/countries/${headquarter[country]}/regions.json
, theheadquarter[country]
field would be deemed required (can't be empty or null).Additionally, a set of query string mappings could be provided for optional query string parameters.
e.g. given this
queryStringMapping
If a dependent field named
headquarter[continental_only]
were to tell the super-select that its value is now set totrue
, then the searchUrl would be changed to/account/countries/1/regions.json?continental_only=true
.The url pattern could also require the
continental_only
query string by specifying it in the pattern directly:/account/countries/${headquarter[country]}/regions.json?continental_only=${headquarter[continental_only]}
And to make these url params persist a back operation, they would all be saved in a
urlParams
value. AnurlParamChanged()
callback would catch the changes to the urlParams value, assemble the new searchValue if all required params are gathered, and thereby updating the super-select's options via that json request.This work into updating the searchUrl from this pattern and mappings could be made into a Stimulus mixin, re-usable into other controllers (e.g. for updating a turbo_frame's
src
).