drgullin / icheck

Highly customizable checkboxes and radio buttons (jQuery & Zepto)
http://fronteed.com/iCheck
7.39k stars 1.63k forks source link

how to prevent ifChecked event in callback #349

Open yanqiw opened 7 years ago

yanqiw commented 7 years ago

Here is my issue: i want to prevent the checkbox checked event base on some condition, when user check the checkbox, i call the ifchecked callback, and validate some condition, if the condition is true, prevent the check. However, on matter i return false or event.preventDefault(), the checkbox always checked.
i got a work around, but i am not sure whether it is a proper way:

function asyncEvent(event){
    var dfd = jQuery.Deferred();
    setTimeout(function () {
      dfd.resolve(event);
    }, 500);
    return dfd.promise();
  }

function removeCheck(event){
  console.log(event.target);
  jQuery(event.target).iCheck('uncheck');
}

jQuery('.input').on("ifChecked",function(event){
  //if(condition){
    //remove
    jQuery.when(asyncEvent(event)).then(removeCheck)}
  //}

);