Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have the following method on my Vue instance:

    ajaxSubmit: function(event) {
        this.loader = true;

        var form = event.target;

        var action = form.getAttribute('action');
        var data = new FormData(form);
        var method = form.getAttribute('method').toUpperCase();

        this.$http({
            body: data,
            method: method,
            url: action
        }).then(function(response) {
            if (response.body.alerts) {
                response.body.alerts.forEach(function(alert) {
                    this.alerts.push(alert);
                }.bind(this));
            }

            this.loader = false;
        }, function(response) {
            // same as success callback
        });
    }

Let's say I have an input with a default value of "Test". If I update it to "Test111" and then submit. The value reverts to "Test". The submitted value is correct, as "Test111" gets saved to my database and also after a refresh, the input has the correct value.

I managed to narrow down the problem. I observed that it only happens if in the ajaxSubmit method I change data on the instance. For example if I delete the lines:

this.loader = true;
this.loader = false;
this.alerts.push(alert);

everything works as expected.

Also, if an input is bound to the instance via v-model, it doesn't revert its value.

So, in the end I guess I found the source of the problem, but I don't understand it, and I can't solve it.

Any help would be greatly appreciated!

share|improve this question
    
Could you also show your Vue instance/component and markup, at the the moment it's difficult to see what the problem could be without seeing what you are trying to do. – craig_h Dec 30 '16 at 18:14
    
@craig_h It's part of a large application, but I managed to put together a stripped down fiddle which replicates the issue: jsfiddle.net/ilpet/v3xx02n9 – Illes Peter Jan 2 at 8:57

The problem you are having is that Vue re-renders the input element after the submit and because you have set a value attribute it will always re-render with the initial value. The way to get around this is to either not have an initial value, in which case Vue will keep the content, or to use v-model and bind it to data:

HTML

<input type="text" name="test" v-model="inputVal" />

JavaScript

...
data:{
  ...
  inputVal: "Test"
  ...
}
...

Here's your updated JSFiddle: https://jsfiddle.net/v3xx02n9/1/

share|improve this answer
    
Is there no other way? I knew it works if I bind it to data, but that is not a solution, as I can't use this method universally, without knowing the fields of the underlying form. Not having an initial value is also not an option, as it is an edit form. – Illes Peter Jan 2 at 14:46
    
@IllesPeter Not totally sure where the obstacle lies, commonly we don't set the initial value in the html, but set the data in vue, and let vue set the value displayed for us. – PanJunjie潘俊杰 Jan 2 at 15:32
    
@PanJunjie潘俊杰 Well, the form is populated by the server-side script, and that's why there's this issue. I can now see that the nicer way would be to do this client-side, but it would be a pretty big effort at this time of the project. – Illes Peter Jan 2 at 17:45
    
@IllesPeter I think you can put the server-side vars in a global js object, like var serverVars = {}; serverVars.someComponent.someData = '{{interpolationByServerSide}}', and put the js file containing vue components after the above code, and then just access them in vue code as global objects. – PanJunjie潘俊杰 Jan 3 at 2:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.