Search code examples
javascriptjqueryicheckremodal

iCheck event fired many times after pairing with Remodal


I am currently using:

  • remodalJS 0.6.4
  • iCheckJS 1.0.2

the UX is designed so that the iCheck checkboxes and radios act as filters and live within a remodal. Weird things happened when I paired up the two libraries to use:

chrome devtool console screenshot

This is the console output from Chrome,

ifChecked is being fired one time more when a selection is checked (by radio or checkbox).

<html>
    <head>
        <link rel="stylesheet" href="bower_components/remodal/dist/jquery.remodal.css">
        <link rel="stylesheet" href="bower_components/iCheck/skins/line/green.css">
    </head>
    <body>
        <h1 data-remodal-target="test-icheck-remodal"> Animal</h1>
        <h1 data-remodal-target="test-icheck-remodal-2"> Comics</h1>
        <div class="remodal" data-remodal-id="test-icheck-remodal">
            <h2>Choose Animal</h2>
            <input type="checkbox" value="">
            <label>Cat</label>
            <input type="checkbox" value="">
            <label>Dog</label>
        </div>

        <div class="remodal" data-remodal-id="test-icheck-remodal-2">
            <h2>Choose Comics</h2>
            <input type="checkbox" value="">
            <label>Batman</label>
            <input type="checkbox" value="">
            <label>Superman</label>
        </div>
        <script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
        <script type="text/javascript" src="bower_components/remodal/dist/jquery.remodal.js"></script>
        <script type="text/javascript" src="bower_components/iCheck/icheck.js"></script>
        <script type="text/javascript">
            $('input:checkbox, input:radio').each(function() {
                var self = $(this),
                label = self.next(),
                label_text = label.text();
                label.remove();
                self.iCheck({
                    checkboxClass: 'icheckbox_line-green',
                    radioClass: 'iradio_line-green',
                    insert: '<div class="icheck_line-icon"></div>' + label_text
                })
            });
            var flag = false;
            $(document).on('open', '.remodal', function(e) {
                var $this = $(this);
                var inputFields = $this.find('input:checkbox, input:radio');

                console.log('remodal Opened');
                if(!flag) {
                    inputFields.on('ifChecked', function(event) {
                        console.log('ifChecked called');
                    });

                    flag = true;
                }

            });
        </script>
    </body>
</html>

Corresponding bower.json

{
  "name": "remodal_icheck",
  "version": "0.0.0",
  "dependencies": {
    "remodal": "0.6.4",
    "iCheck": "1.0.2"
  },
  "devDependencies": {
  }
}

Solution

  • I think the issue here is you keep binding the event everytime the modal is open. There are several fix I can think of, but I'm not very sure if it's the best way to handle it.

    My first solution: Create a flag that when you bind an event set that flag to true, so when it goes again there it wouldn't bind it again.

        // the flag
        var flag = false;
        $(document).on('open', '.remodal', function(e) {
            var $this = $(this);
            var elementId = $this.attr('data-remodal-id');
            var inputFields = $this.find('input:checkbox, input:radio');
            var queryParam = $this.attr('data-query-param');
    
            console.log('remodal Opened');
    
            // check flag if false
            if(!flag) {
              /* If it's radio, Close the remodal after selecting options */
              inputFields.on('ifChecked', function(event) {
                queryObject[queryParam] = 'hihi';       
                console.log('ifChecked called');
                if ($this.has('input:checkbox').length == 0) {
                    $this.remodal().close();
                }
              });
    
              /* Todo: move to function clearFilters() */
              $this.find('.remodal-filter-clear').on('click', function() {
                inputFields.iCheck('uncheck');
              });
    
              flag = true;
            }
    
        });
    

    Update

    Adding second solution based from your comment below, having two .remodal classes.

    We add a class to those input elements which have already bound to that event.

    For this example let's call that classname as bound.

        $(document).on('open', '.remodal', function(e) {
            var $this = $(this);
            var elementId = $this.attr('data-remodal-id');
            // find input elements which don't have the `.bound` class, then add class
            var inputFields = $this.find('input:checkbox:not(.bound), input:radio:not(.bound)').addClass("bound");
            var queryParam = $this.attr('data-query-param');
    
            console.log('remodal Opened');
    
    
              /* If it's radio, Close the remodal after selecting options */
              if(inputFields.length > 0) {
                inputFields.on('ifChecked', function(event) {
                  queryObject[queryParam] = 'hihi';       
                  console.log('ifChecked called');
                  if ($this.has('input:checkbox').length == 0) {
                    $this.remodal().close();
                  }
                });
    
                /* Todo: move to function clearFilters() */
                $this.find('.remodal-filter-clear').on('click', function() {
                  inputFields.iCheck('uncheck');
                });
    
              }
        });