-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Type transformation modifiers for v-model #2002
Comments
@rpkilby Thanks for creating this issue, I was planning on tackling it to day, but work got in the way. :) |
Curious - couldn't this just be done with custom two-way filters? Maybe I'm missing something. |
@davidkhess Indeed, it seems possible: https://jsfiddle.net/posva/8whzuL16/1/ |
@davidkhess Yes, it's possible. This issue, however, is about adding built-in support. The problem I've had with this approach is that it's unexpected, coming from other frameworks. All of them 'have the magic built in' to handle number inputs. Writing your own filter to handle this, while possible, is very sub-optimal. The compromise between what @yyx990803 envisions and what I'd like seems to be adding transforms for |
@Morgul completely understand – I tend to favor less magic. |
@rpkilby @Morgul Do you have a real use-case about any modifier you think should exist? There is already a number parameter for text inputs: http://vuejs.org/guide/forms.html#number |
@posva Not really, no. As discussed in #1713, it's surprising when you're getting a number out of a number input. While vue has a way of handling that (the |
@posva - It seems like a date transform for the date/datetime input types would be useful. However, I'm more interested in being able to register transforms. eg,
|
@rpkilby I think two way filters work out pretty well for this. The guide actually talk about this http://vuejs.org/guide/custom-filter.html#Two-way-Filters |
@posva - yes, and we currently do this. I guess my problem is that I have vague (and I want to emphasize, very minor) dissatisfactions with the API for dealing with various data types. I have some thoughts that haven't really formed into anything cohesive.
The result of all of these leaves us with: <input type="checkbox" v-model="foo" />
<input type="number" v-model="bar" number />
<input type="date" v-model="baz | date" />
Example: <input type="text" v-model="title" />
<input type="text" v-model="slug | lower | kebab" lazy />
<input type="date" v-model.date="publishOn" />
<input type="number" v-model.number="priority" /> Edit:
|
@rpkilby very nice analysis and writeup. Agreed, things are inconsistent. |
@rpkilby thanks for the thoughtful input - we'll definitely improve this in 1.1 :) |
@yyx990803 any new thoughts on this given the deprecation of filters outside of interpolated text in Vue 2.x? |
Ditto, removing filters seems to beg for some hook on v-model to accomplish type/value transformations. |
The problem with other type transformations other than Type transformations also by definition makes the view out of sync with the underlying state. My take on this use case is that instead of the implicit magic conversions, let For example, you can perform these transformations only when you need to send it to the server for persistence. Or, use computed properties based on the bound value if you need to display it elsewhere in a different format. |
Preface: upon rereading, it's unclear if you're talking about removing the For simple use cases, this may be sufficient (and in some cases preferred). However, I have a use case with about 60 I agree in principal with "let I'm fine if I have to do some work somewhere (specifying a transform, a two way filter, or something like that), but I don't want to have to define 60+ computed properties and then write code to try and merge those computed properties with another object just so I can turn it into JSON to send to my server. It feels like an inelegant solution, compared to other frameworks. My use case is numeric inputs, but being a little more pedantic, you could apply the same logic to the values for check boxes (true/false vs string values), date fields, radio buttons, etc. Any time you have a large number of properties that need special handling, you'll run into this problem. I just don't think that 'use a computed property' scales well. I'd be a lot more sanguine if it felt like there was a clear path forward that had the same elegance I'm used to in the rest of the framework. |
@Morgul |
For argument:
For a one-off type transformation this is fine, but as @Morgul said, this is problematic at scale. In our latest project we dealt with a lot of dates. In lieu of type transforms, we used two-way filters. At some point, we made the switch to using moment.js. Updating the filter was incredibly simple.
In contrast, using computed properties would have been more painful:
Other thoughts:
I would disagree - the view is a representation of the underlying state. The type transformation simply transforms an unrepresentable type (Date object) into something that is representable (date string).
Type transformations are definitely similar to two-way filters, however they seem like they would be much more simple. An intentionally minimal API makes sense.
Possible API: Vue.typeTransform('date', {
render(value) {
return value ? value.isoformat() : '';
},
parse(value) {
return value ? new Date(value) : undefined;
},
}); |
This would be in line with the coerce functionality in component props. |
A boolean modifier would have been great. |
This works perfect, indeed a boolean modifier would also be a nice addition 👍 |
Az
…On Wed, 1 Aug 2018, 18:31 Jerry Jacobs, ***@***.***> wrote:
This works perfect, indeed a boolean modifier would also be a nice
addition 👍
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2002 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AOYJoQCojZNAjrbZxY_4QLHVHItaRZCqks5uMeYLgaJpZM4Gy4Xw>
.
|
From @Morgul's comment in #1713:
It would also seem useful to be able to register custom transformations. eg, datetime transformations for Moment.js objects.
The text was updated successfully, but these errors were encountered: