Matt Westlake
Matt Westlake

Reputation: 3651

how do you keep meteor from clearing form on update

I have a meteor / angular app with my index.ng.html page looking like

<div>
    {{1+4}}
    <form id = "myForm" name="myForm" >
        <input type="text" name = "username"/>
        <input type="text" name = "password"/>
        <input type="submit" value="Submit"/>
    </form>
</div>

When I update the file by changing 1+4 to 1+6, any pre-filled information I have in my form is lost when the page "reloads".

How can I keep from loosing data when the page reloads?

Upvotes: 0

Views: 70

Answers (1)

Tarang
Tarang

Reputation: 75945

Store the data in a Session object. This should survive a hot code reload but not a manual page refresh.

Template.xxx.events({
    'change input': function(e,tmpl) {
        var fieldName = tmpl.$(e.currentTarget).attr("name");

        Session.set('field' + fieldName, e.currentTarget.value);
    }
});

Template.xxx.helpers({
    valueForField = function(fieldName) {
        return Session.get("field" + fieldName);
    }
});

Then you can use this in your html:

<form id = "myForm" name="myForm" >
        <input type="text" value="{{valueForField 'username'}}" name = "username"/>
        <input type="text" value="{{valueForField 'password'}}" name = "password"/>
        <input type="submit" value="Submit"/>
    </form>

It's obviously a bit tedious. Meteor used to do this natively using a 'preserve-inputs' package, but it looks to be deprecated.

Upvotes: 1

Related Questions