Skip to content

Commit

Permalink
Added event triggering.
Browse files Browse the repository at this point in the history
Any calls to `check`, `uncheck`, `toggle` and `range` will trigger `change`
events to affected checkboxes.
  • Loading branch information
rmariuzzo committed Oct 1, 2016
1 parent 118e311 commit a61bf47
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
8 changes: 4 additions & 4 deletions dist/jquery.checkboxes-1.1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
_createClass(Checkboxes, [{
key: 'check',
value: function check() {
this.$context.find(':checkbox').filter(':not(:disabled)').filter(':visible').prop('checked', true);
this.$context.find(':checkbox').filter(':not(:disabled)').filter(':visible').prop('checked', true).trigger('change');
}

/**
Expand All @@ -40,7 +40,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
}, {
key: 'uncheck',
value: function uncheck() {
this.$context.find(':checkbox:visible').filter(':not(:disabled)').prop('checked', false);
this.$context.find(':checkbox:visible').filter(':not(:disabled)').prop('checked', false).trigger('change');
}

/**
Expand All @@ -53,7 +53,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
this.$context.find(':checkbox:visible').filter(':not(:disabled)').each(function (i, element) {
var $checkbox = $(element);
$checkbox.prop('checked', !$checkbox.is(':checked'));
});
}).trigger('change');
}

/**
Expand Down Expand Up @@ -110,7 +110,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
var start = Math.min(from, to);
var end = Math.max(from, to) + 1;

$checkboxes.slice(start, end).filter(':not(:disabled)').prop('checked', $checkbox.prop('checked'));
$checkboxes.slice(start, end).filter(':not(:disabled)').prop('checked', $checkbox.prop('checked')).trigger('change');
}
instance.$last = $checkbox;
});
Expand Down
12 changes: 8 additions & 4 deletions src/jquery.checkboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
this.$context.find(':checkbox')
.filter(':not(:disabled)')
.filter(':visible')
.prop('checked', true);
.prop('checked', true)
.trigger('change');
}

/**
Expand All @@ -32,7 +33,8 @@
uncheck() {
this.$context.find(':checkbox:visible')
.filter(':not(:disabled)')
.prop('checked', false);
.prop('checked', false)
.trigger('change');
}

/**
Expand All @@ -44,7 +46,8 @@
.each((i, element) => {
let $checkbox = $(element);
$checkbox.prop('checked', !$checkbox.is(':checked'));
});
})
.trigger('change');
}

/**
Expand Down Expand Up @@ -90,7 +93,8 @@

$checkboxes.slice(start, end)
.filter(':not(:disabled)')
.prop('checked', $checkbox.prop('checked'));
.prop('checked', $checkbox.prop('checked'))
.trigger('change');
}
instance.$last = $checkbox;
});
Expand Down
4 changes: 4 additions & 0 deletions tests/specs/jquery_checkboxes_check_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('The `check` method', function () {
});

it('should check all visible and enabled checkboxes in context', function () {
var spyEvent = spyOnEvent(ctx.modified, 'change');

// Check all checkboxes in context.
ctx.modified.checkboxes('check');

Expand All @@ -38,6 +40,8 @@ describe('The `check` method', function () {
expect(modifiedState).toBe(originalState);
}
});

expect(spyEvent).toHaveBeenTriggered();
});

});
4 changes: 4 additions & 0 deletions tests/specs/jquery_checkboxes_toggle_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('The `toggle` method', function () {
});

it('should toggle all visible and enabled checkboxes in context', function () {
var spyEvent = spyOnEvent(ctx.modified, 'change');

// Toggle all checkboxes in context.
ctx.modified.checkboxes('toggle');

Expand All @@ -38,6 +40,8 @@ describe('The `toggle` method', function () {
expect(modifiedState).toBe(originalState);
}
});

expect(spyEvent).toHaveBeenTriggered();
});

});
4 changes: 4 additions & 0 deletions tests/specs/jquery_checkboxes_uncheck_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ describe('The `uncheck` method', function () {
});

it('should uncheck all visible and enabled checkboxes in context', function () {
var spyEvent = spyOnEvent(ctx.modified, 'change');

// Uncheck all checkboxes in context.
ctx.modified.checkboxes('uncheck');

Expand All @@ -38,6 +40,8 @@ describe('The `uncheck` method', function () {
expect(modifiedState).toBe(originalState);
}
});

expect(spyEvent).toHaveBeenTriggered();
});

});

0 comments on commit a61bf47

Please sign in to comment.