与 Remodal 配对后多次触发 iCheck 事件
iCheck event fired many times after pairing with Remodal
我目前正在使用:
- remodalJS 0.6.4
- iCheckJS 1.0.2
UX 的设计使 iCheck 复选框和收音机充当过滤器并在重塑中生活。当我将两个库配对使用时发生了奇怪的事情:
这是 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>
对应bower.json
{
"name": "remodal_icheck",
"version": "0.0.0",
"dependencies": {
"remodal": "0.6.4",
"iCheck": "1.0.2"
},
"devDependencies": {
}
}
我认为这里的问题是每次模式打开时你都在绑定事件。我可以想到几个修复方法,但我不太确定这是否是处理它的最佳方法。
我的第一个解决方案:
创建一个标志,当你绑定一个事件时将该标志设置为 true,这样当它再次出现时它就不会再次绑定它。
// 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;
}
});
更新
根据您在下面的评论添加第二个解决方案,有两个 .remodal
classes.
我们向那些已经绑定到该事件的输入元素添加一个 class。
对于此示例,我们将 class 名称称为 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');
});
}
});
我目前正在使用:
- remodalJS 0.6.4
- iCheckJS 1.0.2
UX 的设计使 iCheck 复选框和收音机充当过滤器并在重塑中生活。当我将两个库配对使用时发生了奇怪的事情:
这是 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>
对应bower.json
{
"name": "remodal_icheck",
"version": "0.0.0",
"dependencies": {
"remodal": "0.6.4",
"iCheck": "1.0.2"
},
"devDependencies": {
}
}
我认为这里的问题是每次模式打开时你都在绑定事件。我可以想到几个修复方法,但我不太确定这是否是处理它的最佳方法。
我的第一个解决方案: 创建一个标志,当你绑定一个事件时将该标志设置为 true,这样当它再次出现时它就不会再次绑定它。
// 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;
}
});
更新
根据您在下面的评论添加第二个解决方案,有两个 .remodal
classes.
我们向那些已经绑定到该事件的输入元素添加一个 class。
对于此示例,我们将 class 名称称为 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');
});
}
});