You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/guide/migration.md
+42Lines changed: 42 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1014,6 +1014,48 @@ In many cases though, you'll still run into strange behavior (e.g. `0.035.toFixe
1014
1014
</div>
1015
1015
{% endraw %}
1016
1016
1017
+
### Two-Way Filters <sup>deprecated</sup>
1018
+
1019
+
Some users have enjoyed using two-way filters with `v-model` to create interesting inputs with very little code. While seemingly simple however, two-way filters can also hide a great deal of complexity - and even encourage poor UX by delaying state updates. Instead, components are recommended as a more explicit and feature-rich way of creating custom inputs.
1020
+
1021
+
We'll now walk through an example of a filter provided by the community and offer tips for how to migrate away from two-way filters. Here's a simple currency filter:
It mostly works well, but the delayed state updates can cause strange behavior. For example, try entering `99.999` into one of those inputs. When the input loses focus, its value will update to `$100.00`. When looking at the calculated total however, you'll see that `99.999` is what's stored in our data. The version of reality that the user sees is out of sync!
1026
+
1027
+
To start transitioning towards a more robust solution using Vue 2.0, let's first refactor this filter into a component that uses it:
This allows us add behavior that wasn't possible with a filter alone, such as selecting the content of an input on focus. Now the next step will be to extract the business logic from the filter:
This increased modularity not only makes it easier to migrate to Vue 2, but also allows currency parsing and formatting to be:
1036
+
1037
+
- unit tested in isolation from your Vue code
1038
+
- used by other parts of your application, such as to validate the payload to an API endpoint
1039
+
1040
+
Having this validator extracted out, we can also more comfortably build it up into a more robust solution. We've now eliminated the state quirks and have even made it impossible for users to enter anything wrong, similar to what the browser's native number input tries to do.
1041
+
1042
+
We're still limited however, by filters and by Vue 1.0 in general, so let's complete the upgrade to Vue 2.0:
- Every aspect of our input is more explicit, using lifecycle hooks and DOM events in place of the hidden behavior of two-way filters.
1049
+
- We can now use `v-model` directly on our custom inputs, which is not only more consistent with normal inputs, but also means our component is Vuex-friendly.
1050
+
- Since we're no longer using filter options that require a value to be returned, our currency work could actually be done asynchronously. That means if we had a lot of apps that had to work with currencies, we could easily refactor this logic into a shared microservice.
1051
+
1052
+
{% raw %}
1053
+
<divclass="upgrade-path">
1054
+
<h4>Upgrade Path</h4>
1055
+
<p>Run the <ahref="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of filters used in directives like `v-model`. If you miss any, you should also see <strong>console errors</strong>.</p>
0 commit comments