Skip to content

Commit ca06c02

Browse files
committed
add two-filters section to migration guide
1 parent 49b50a3 commit ca06c02

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/guide/migration.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,48 @@ In many cases though, you'll still run into strange behavior (e.g. `0.035.toFixe
10141014
</div>
10151015
{% endraw %}
10161016

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:
1022+
1023+
<iframe width="100%" height="300" src="https://jsfiddle.net/chrisvfritz/6744xnjk/embedded/result,html,js" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
1024+
1025+
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:
1028+
1029+
<iframe width="100%" height="300" src="https://jsfiddle.net/chrisvfritz/g7osemrx/embedded/result,html,js" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
1030+
1031+
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:
1032+
1033+
<iframe width="100%" height="300" src="https://jsfiddle.net/chrisvfritz/j4xtb20e/embedded/result,html,js" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
1034+
1035+
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:
1043+
1044+
<iframe width="100%" height="300" src="https://jsfiddle.net/chrisvfritz/r4e49aLs/embedded/result,html,js" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
1045+
1046+
You may notice that:
1047+
1048+
- 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+
<div class="upgrade-path">
1054+
<h4>Upgrade Path</h4>
1055+
<p>Run the <a href="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>
1056+
</div>
1057+
{% endraw %}
1058+
10171059
## Slots
10181060

10191061
### Duplicate Slots <sup>deprecated</sup>

0 commit comments

Comments
 (0)