Call event.preventDefault() if error happened in Meteor method

I’m trying to stop the default behavior of the click event handler if error happened during the execution of some Meteor method which i’m calling using Meteor.call(), the method throws an Exception if error happened, but i can’t find the right way! I tried to put Meteor.call() in a try/catch block but nothing got caught:

'click .toggle-checked' (event) {
        try {
            Meteor.call('tasks.setChecked', 1, !this.checked);
        } catch (e) {
            console.log("Error caught!");
            event.preventDefault();
        }
},

Also I tried to pass a callback to Meteor.call() that stops the event default behavior but it didn’t work either, it seems that the event handler finishes its work before the callback get called:

'click .toggle-checked' (event) {
        Meteor.call('tasks.setChecked', 1, !this.checked, function (error, result) {
            if(error) {
                event.preventDefault();
                console.log("error happened!", error, event);
            }

        });
        console.log("getting out!");
},

What you need to do is prevent default at the start, then trigger the event again. You’ll need to keep a flag to tell whether you triggered the Meteor call already or not:

'click .toggle-checked' (event, instance) {
    event.preventDefault();
    if (instance.callSuccessfullFlag === true) {
         instance.$('.toggle-checked').trigger('click');
         instance.callSuccessfullFlag = false; // reset for next event
    } else {
        Meteor.call('tasks.setChecked', 1, !this.checked, function (error, result) {
            if(error) {
                console.log("error happened!", error, event);
                return;
            }
            instance.callSuccessfullFlag = true;
        });
    } 
},

You would need to be careful that the new click event doesn’t end up unticking the checkbox.

This level of workaround shouldn’t be needed though, why do you have two event handlers on the same element+event combo?

Can you combine it in one? Or move the functionality off to another module?

That way you could just do:

'click .toggle-checked' (event) {
    Meteor.call('tasks.setChecked', 1, !this.checked, function (error, result) {
         // do the other thing here instead of in another handler
    });
},