Skip to content

Approach of conversion for array in query to avoid breaking change #817

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

Merged
merged 9 commits into from
Mar 14, 2017

Conversation

KingMario
Copy link
Contributor

@KingMario KingMario commented Mar 10, 2017

related to: vuejs/vue-router#1232

```javascript
'$route.query.users': {
handler(val) {
this.$route.query.users = Array.isArray(val) ? val : [val]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we talk about watchers? I think the relevant part is that the user should check if val is an array, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@posva This watcher can help to convert the query to an array no matter how many elements are provided in the url.

```javascript
'$route.query.users': {
handler(val) {
this.$route.query.users = Array.isArray(val) ? val : [val]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't talk about watchers, simply show the example like:

const users = Array.isArray(this.$route.query.users)
  ? users
  : [users]

Modifying the $route object is a bad idea because it's like a snapshot of the current state, it's supposed to be read only

@posva
Copy link
Member

posva commented Mar 13, 2017

Sorry if I wasn't clear enough in my requests. I updated the part, could you check to see if I'm missing something?

@posva
Copy link
Member

posva commented Mar 13, 2017

Could you please revert that last commit? It's easier to keep the example simple to make easy to match every one case. If you want to use a computed property in the example, please explain why

```

And then replace the query reference `this.$router.query.users` to the computed `users` in the context accordingly.
If you were relying on `$route.query.users` in your template, you could use this inside of a computed property. If you were using the query result in a watcher, you should apply the check there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The query users could be used anywhere. When a computed property is added, just replacing every this.$router.query.users with the computed users is sufficient and straightforward.

Adding the computed property is to separate and simplify the checking if the query array is used MULTIPLE TIMES in a component no matter in the script block or the template block, and no obvious defect if it's a one-time-usage.

Besides, if the user is FOR SURE the query parameter SHOULD BE an array during the migration from vue-router 0.7 to vue-router 2, EVEN adding a watcher is acceptable, to my opinion. It will not break the query's consistency, but consolidate the query's consistency.

Since vue-router will treat `?users=Tom` as `query.users == 'Tom'`, unless you are for sure that the array will have more than one element, you should check and convert the query to array if necessary.

One practical way is to add a computed property like this:
When passing arrays to query parameters the syntax is no longer `/foo?users[]=Tom&users[]=Jerry`, instead, the new syntax is `/foo?users=Tom&users=Jerry`. Internally, `$route.query.users` will still be an Array, but if there's only one parameter in the query: `/foo?users=Tom`, when directly accessing this route, there's no way for the router to know if we were expecting `users` to be an Array. Because of this, consider using the following check:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the syntax is about URL, or QueryString (if URL is not mentioned).

@KingMario
Copy link
Contributor Author

@posva Explained in the review of your commit.

@KingMario
Copy link
Contributor Author

@posva And I intended using watcher, which is really effortless in resolving the problem caused by such a breaking change during migration. And I still think it's the best practice.

@posva
Copy link
Member

posva commented Mar 13, 2017

@KingMario 👍 Updated some lines to polish it

Copy link
Member

@posva posva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KingMario
Copy link
Contributor Author

@posva Thx

@KingMario
Copy link
Contributor Author

Still think adding watcher 一劳永逸
:(

@kazupon kazupon merged commit 08f0ca0 into vuejs:master Mar 14, 2017
@kazupon
Copy link
Member

kazupon commented Mar 14, 2017

@KingMario
You can send the pull request :)

@posva
Copy link
Member

posva commented Mar 14, 2017 via email

kazupon pushed a commit to kazupon/vuejs.org that referenced this pull request Oct 1, 2017
…uejs#817) (vuejs#238)

* Approach of conversion for array in query to avoid breaking change (vuejs#817)

Apply original diffs

* translate into Japanese

* delete "changed": tracking the original
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

Successfully merging this pull request may close these issues.

3 participants