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
autoNumeric + Vue.js 2.0 : autoNumeric does not see value updates made via Vue #247
Comments
Alexandre, Is vue.js updating the second input with .val() method? If so that would cause the issue you are having. In version 2.0 the "set" method is required to update the input and rawValue which is stored on jquery data. On focus autoNumeric checks the rawValue and compares that to the input value and if their is a difference the rawValue is used. This was implemented to deal with ASP.net postback. Bob |
If I understand 'asp.net's postback' right, it's like Angular 1 which always kept a 'view' value that is shown on the web page, and a 'real' value which it kept up-to-date by the engine, right? Well, wouldn't it be a good goal to try to keep autoNumeric as much framework/technology agnostic as possible? So, this behavior poses problem with very basic and common thing like updating an input by setting its value directly. Would it be possible, specially since asp.net is not as widely used as vanilla JS/Jquery/Vue.js/Angular/etc. to either :
This change would allow all framework users to just continue using data-binded inputs without having to deal with multiple (and borderline impossible to forecast) dependencies between inputs. |
Ok, I just tested the previous codepen by using v1.9.46, and indeed it works as intended (which is great and simple!). Would it be possible to either revert this back and put an option for asp.net users, or add an option for the rest of us to get that behavior back? |
I like your idea of having an option that is ASP.net specific. Modifying the anDefault that checks the rawValue versus the input value seems logical or adding a new option is also an alternative. Will keep you informed. |
Had some time to think about this. what happens on post back is the original default value is overwritten so there is no way the autoNumeric could detect if the page was loading for the first time hence the "anDefault" option was added. The "rawValue" was created to have a number readily available so the input would not need to be stripped. Most developers are not going to format a value the populate the inputs with the .val(). The "set" method makes it easy - IMHO. |
I'm not sure to follow your explanation about post back and initial page loading. I understand that autoNumeric keeps a On the other hand, I don't see how that affect the fact that on focus, autoNumeric should by default, in my opinion, just use the input value over the Do you still plan to allow the deactivation of the asp.net specific behavior, one way or another? |
Alexandre, I am enjoying our conversation - it's always nice to get different and new views and ideas. On page load autoNumeric compares the default value with the actual value of the input. If they are the same autoNumeric will format the input value. What happens with ASP.Net page is on postback the page is re-rendered changing the default value to that of the input value so autoNumeric attempts to format an value that's already formatted causing errors. // user enters "1234.56" => "1,234.56" with default settings I created the option "anDefault" that should be set to the same value as the default value and will retain the same value on postback. Concerning the rawValue - I see you point with the possible exceptions:
If I change the code to function as you suggest do I make exceptions for the above two options? And if a developer can handle these exceptions then why can't they just use the "set" method? for updating the input? Just playing the devils advocate. Look forward to you reply Bob |
For anyone that might be looking to use autoNumeric with Vue.js, I made a component that solves a few minor quirks I had to deal with. No need to modify autoNumeric. You can check it out here. It does require autoNumeric 2.0 to work. |
Carlos. Thank you for your contribution - much appreciated. Bob |
To all, I have no problem with modifying autoNumeric. Eliminating a few lines of code on v 2.0 will fix the Vue.js issue on most most options but would create issue with "e.Dec" and the new "aScale" options that have multiple view states that rely on the "rawValue" option. IMHO - ultimately it boils down to how easy it is to use the .val() versus autoNumeric "set" method. So I ask that for your suggestions and options. Bob |
Honestly, looking at @AlexandreBonneau's example, it seems to work ok, but it's overly complicated for something that should be rather simple to implement. I think it's very common for people wanting to use a standalone (or jQuery dependent) library with a modern JS framework to make a wrapper library that takes care of the small incompatibilities that are pretty much always present. autoNumeric should be no exception in this regard. My component is pretty much done, pending issue #251 and I couldn't really ask for more. |
Hello guys, I'm glad to see some more activity to make the great autoNumeric plugin even better! Let me answer you both. @BobKnothe :
Yes if the developer change one single and isolated input.
Not having seen any documentation about those, I'm not sure exactly how that works with our problem.
I cannot say anything about the second sentence talking about 'eDec' and 'aScale'.
The developer unfortunately cannot easily manage the first exception you talk about when there are multiple dependent inputs (if its even possible without a lot of additional complex code).
Oh, I'm sorry to hear that :/
Well, as I have written above, you can not manually @rhyek :
Yes, but only when using v1.9.46. v2 breaks the databinding feature of Vue.js :( That said, your component is great and close to parity with my directive, but I think its too focused and that it lacks some important features. First, as you mentioned, it does not support pasting ; if you paste a number in the input, the v-model does not get updated. Also some minor notes I could add ;
Secondly, and more importantly, my main grip is about using a component to manage autoNumeric. What I like about your component though, is that the v-model always contains the unformatted value. tl;dr :
|
@AlexandreBonneau, I appreciate the code review. Some clarifications, though:
It does get updated. You can clearly see it in action on the fiddle I linked earlier.
Good catch.
We're already using jQuery to initialize the
I hadn't considered this, but you still have to initialize the element in the
What do you mean by limited behavior? If you mean adding other html attributes you can already do that, eg:
|
Sorry but it does not. Take a look at what's really going on using vue-devtools.
This goes against the MVVM pattern that Vue.js follows, but I guess you can do whatever you want :)
I do not have time to create a performance test, but by reading the doc thoroughly, it appears it does, specially if you managed multiple autoNumeric elements.
Well, I think I explained it in the previous post. If you only plan to have a basic autoNumeric element, then I guess a component would do, but if you want to have the flexibility to add more behaviors (not "html attributes"), a directive is the way to go.
Your personal experience is yours, and yours only. I'm pretty sure that if autoNumeric allows the use on 'b', 'caption', 'cite', 'code', 'dd', 'del', 'div', 'dfn', 'dt', 'em', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ins', 'kdb', 'label', 'li', 'output', 'p', 'q', 's', 'sample', 'span', 'strong', 'td', 'th', 'u' and 'var' elements, it's because some other users needs that. I know I'm big fan of
The same way I'm not a fan that autoNumeric depends on jQuery, I fail to see why you would need an additional formatting library while you already has autoNumeric?! I like the
Yes, the codepen you are refering to includes the code that manage all paste cases, yours doesn't. On the screenshot and to compare apples with apples, I removed the code that manage the paste events to get the same limited scope as your component. |
@AlexandreBonneau, like I mentioned earlier: You are using code from an old version of that fiddle. If you check again you will see a number of changes. v-model does get updated on paste. I will unsubscribe from this thread now since it's getting kind of annoying. Hopefully someone finds the component useful. |
Just a couple of things:
This seems interesting to me. Can you provide a link to the docs where they mention this? I can't find anything and I just can't imagine how moving the attachments to the template provides anything other than a variation in aesthetics. You're not skipping any work during page render doing that.
Can you explain exactly what you mean by "more behaviors"? In your previous comment you mentioned:
You do realize you can treat a component like any other html element, yes? Anything you can do with a Example using the updated fiddle: <autonumeric
tagName="span" <--- choose your tagName or skip to use an input!
style="color:red;" <--- native html style attribute
v-model="value"
:options="{mDec: 2}"
onclick="alert('you clicked me!');"/> <--- native html event attribute Anyways, the Vue.js docs clearly state:
I don't think you can justify the use of directives for this case at all, but it is up to the developer in any case. IMO, autoNumeric is fine (pending issue #251). I definitely don't think it is necessary to remove or hinder new functionality in version 2.0 just to get it working with Vue. |
@AlexandreBonneau, also, I don't understand how your directive is meant to help people who want to format a If you read the section for
|
Gentleman - here is a quick demo of the "e.Dec" option http://codepen.io/RJ_Knothe/pen/mOVpQP that demos the extended decimal places. When the input does not have focus the extended decimal places is stored in "rawValue". So if the developer uses .val() to update the input the the problem is created. on focusin. I can change "aN" so that a developer can overwrite the input value using .val() and this would work on regular "aN" inputs. But "eDec" and "aScale" both require the use of rawValue. Is it simply telling the developer to use only the "set" method on there two inputs? Bob |
On the codepen sample try focusin and focusout so you can see the behavior |
I personally have no issue using It's like setter properties in other languages. If you have something like: class Dog {
private int age;
public int Age {
get { return age; }
set {
if (value > 20) {
throw new Exception("Woah! Dogs don't live that long!!");
}
age = value;
}
}
} You wouldn't directly set Also, when using js frameworks with databinding, you usually want to work with In any case, could you read what I posted on issue #251? Thank you! |
I made a mod for the Vue.js issue that IMHO is a good compromise Can you test and close the issue if you are satisfied Bob |
@AlexandreBonneau, btw, with the latest 2.0 commit, you don't need your function onInput() {
updateVModel();
}
el.addEventListener("input", onInput, false); Hope it helps. |
Guys, you are awesome :) I just had one holiday day, and when I come back, everything is working again with 2.0, nice! :D So I'm not sure it's needed that I answer your previous posts, since it seems from what I just read and saw that you managed to figure out a way to make both our methods (directives and components) work. Quickly ;
Anyhow, thank you again for your work and inputs on that matter. |
Indeed. I think @BobKnothe made a couple formatting changes to the code that might've changed something. I have a new pull request (#257) that reintroduces the fix, plus a couple others. |
@AlexandreBonneau, I have created an issue about the behavior you're seeing. #259 |
@AlexandreBonneau, the changes have been merged with this commit. Could you try again, please? |
Well, this codepen is using the rawgit link so it should use the last commited version for 2.0, but the Well, in fact it does nothing when I try to paste in a input defined like that : <input type="text" v-cdz-numeric v-model="a">
<input type="text" v-cdz-numeric="{ aPad: false }" v-model="b">
<input type="text" v-cdz-numeric="{ aSep: '.', aDec: ',', altDec: '.', aSign: '', pSign: 's', mRound: 'U' }" v-model="c"> while the paste works as intended when used on inputs defined like that : <input type="text" v-cdz-numeric="{aSep: '', aDec: '.', altDec: ',', aSign: '', pSign: 's', mRound: 'U', vMin: '-9999999999.99', vMax: '9999999999.99', aPad: false}" v-model="years">
<input type="text" v-cdz-numeric="{aSep: '', aDec: '.', altDec: ',', aSign: '', pSign: 's', mRound: 'U', vMin: '0', vMax: '9999999999', aPad: false}" v-model="months"> I'm not entirely sure why it behave like this. Note : back in the days when I used Angular 1, I stumbled upon the exact same problem where autoNumeric used alone would behave correctly (sending |
By the way, I'm closing this issue since the commit from Bob solved the 'autoNumeric 2 + value update made by Vue' problem. |
@AlexandreBonneau, hm. Rawgit caches files, btw. Using the development link in theory will update the file minutes after a new change, but who knows. I've created a codepen using the latest file and everything's working as intended. As you can see, I'm only using the Anyways, good luck. |
I had a similar issue, please check #217. I tried autoNumeric v2.0 BETA with @rhyek codepen (transform it into an Ractive decorator) but it still does Is there any change an "afterConvert" callback to be implemented in the autoNumeric core? |
@kouts, can I see your code? |
Here is the fiddle using the "modified" autoNumeric version: http://jsfiddle.net/ufwzyh9c/1/ |
@kouts, I'll check it out later today. Thanks. |
@kouts, can you try my codepen out? It's pointing to my fork which I updated just now. There was a bug when having Let me know if it works for you so I can post a pull request. Thanks. @AlexandreBonneau this should solve your issue as well. The link to my file is https://rawgit.com/rhyek/autoNumeric/master/autoNumeric-2.0/autoNumeric-2.0-BETA.js |
Thanks @rhyek I tested it and I confirm your fork works with latest Ractive (0.8.4). |
@kouts, great! Good to hear. |
Posted pull request #260. |
@rhyek I'm sorry but the second input does not update the first one in your codepen, this only works for the first input. Maybe this is an Ractive problem though? |
@rhyek can you post a similar example using autoNumeric as Vue's custom directive (which is similar to Ractive's decorator)? |
@kouts, this is the closest I could get it to my component's functionality. It's kind of annoying because if you type in the middle of an existing number the caret is moved to the right. This is something I can easily avoid using a component. But there's your example. It works. I would have to assume it's a Ractive issue since even typing in the second input doesn't work. |
Ok @rhyek thanks for your help. |
@kouts no problem. If you ever figure out what the issue is exactly I'd like to hear about it. |
I'm having trouble making autoNumeric work well with Vue.js 2.0.
Everything looks like it's working (even pasting!), but with one small caveat ; having two inputs,
v-model
(the javascript variable that Vue databind to the html element, which mean that when you change the data in one input, all the other elements sharing the samev-model
sees their values updated accordingly),...whenever you input a number in the first input, both inputs shows the updated value, but, if you focus on the second input, autoNumeric reverts the updated value to the old one it somehow 'stored' at one point.
It seems Vue.js updates the input value, but autoNumeric does not see that change happening, hence it somehow keeps the previous value and shows it on focus.
cf. the codepen where you can see that happening.
Would you see how to fix that?
Does autoNumeric store the input value that gets displayed on focus?
Is there a way or a function to update that value when modifying the input value via javascript, and not via a normal user input?
The text was updated successfully, but these errors were encountered: