Setting ReactiveVar in loginWithPassword callback function


#1

Hi! Have a problem with setting ReactiveVar in loginWithPassword callback function - my template doesn’t see changes.

Template.login.onCreated(function() {
    this.loginError = new ReactiveVar(false);
    //this.loginError.set(true);
});
Template.login.events({
    'submit #login_form': function(e) {
        e.preventDefault();
        //Template.instance().loginError.set(true);
    }
});
Template.login.onRendered(function() {
    const instance = this;
    this.$('#login_form').validate({
        errorElement: 'span',
        errorClass: 'input-field_error',
        rules: {
            email: {
                required: false,
                email: false
            },
            password: {
                required: false
            }
        },
        messages: {
            email: {
                required: "Не оставляйте это поле пустым.",
                email: "Проверьте корректность e-mail."
            },
            password: {
                required: "Не оставляйте это поле пустым."
            }
        },
        submitHandler: function(form) {
//            instance.loginError.set(true);
            console.log(instance.loginError.get());
            var email = $('#login-email').val(),
                password = $('#login-password').val();
            Meteor.loginWithPassword(email, password, function(error) {
                if (error){
                    instance.loginError.set(true);
                    console.log(error);
                }
            });
        }
    });
});
Template.login.helpers({
    loginError: function() {
        return Template.instance().loginError.get();
    }
});

In HTML-template I have:

{{#if loginError}}
   <script>
        Materialize.toast({{loginError}}, 3000);
   </script>
{{/if}}

If I set ReactiveVar before call Meteor.loginWithPassword - all works good. What I do wrong or it’s a bug?


#2

Don’t get it… Why not just do the following?:

Meteor.loginWithPassword(email, password, function(error) {
                if (error){
                    Materialize.toast(error), 3000);
                }

Trying to inject scripts using the template (after page is loaded) is never going to work…


#3

It works if I set reactive var before call loginWithPassword function. Problem is not in script tag, the same problem with simple html tags or text.