Skip to content
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

Read-only trackedParam #2

Open
jelhan opened this issue May 17, 2023 · 3 comments
Open

Read-only trackedParam #2

jelhan opened this issue May 17, 2023 · 3 comments

Comments

@jelhan
Copy link

jelhan commented May 17, 2023

A read-only version of trackedParam decorator would be great. I understand that having more than one active component (or controller, route, service, ...) managing the same query parameter could be a footgun. Preventing it by API design is a good decision in my opinion. But more than one component (or controller, route, service, ...) should be able to read the current value to prevent property drilling or services only needed to passthrough query parameter read access.

@ef4
Copy link
Owner

ef4 commented May 17, 2023

The reason trackedParam is a property decorator is that the property is the true owner of the state. Since that would not be true for this read-only one, I think it would be better if it was something like a resource.

But also, I'm not convinced it's a good idea. The way I'm thinking about trackedParam, it doesn't change the data flow within the application. It works like normal tracked, and can even be normal tracked in environments without global routing.

If you want to share a tracked value across disparate parts of an application, you can use @tracked on a service. The same is necessarily true of @trackedParam. I think that would be better than offering a special-purpose read-only API.

@jelhan
Copy link
Author

jelhan commented May 18, 2023

The reason trackedParam is a property decorator is that the property is the true owner of the state.

Shouldn't the URL be the true owner of the state and trackedParam only be a tracked proxy of the value in the URL? Or is there a limitation of the browser's APIs preventing us from reliable detect all changes to the URL?

@ef4
Copy link
Owner

ef4 commented Mar 29, 2025

(I'm picking up some work on this addon after a long time working on other things.)

To followup on this older discussion: the case where the URL is the true owner of the state is also a reasonable case, but it's a different case from what this addon is for.

When the user is typing in a field and that field is being copied to a param in the URL, the URL really is not the authoritative source of state. The user's <input> is. That's the case this addon is for.

The other case, where the URL is the authoritative source, is already how normal route params are handled. In:

this.route('somewhere', { path: '/somewhere/:id' });

The id param is URL-owned authoritative state. The only way to change it is a transition, and your code handles that change by re-executing the model hook.

I think we should make it possible to do:

this.route('somewhere', { path: '/somewhere?q=:id' })

for the case where the URL should own a param, but the param is optional and appears in a search part of the URL. This should be just like a regular route param, except it's not mandatory for the route match and your model hook may receive the value undefined when it's not present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants