From a61bf474007c4697e0408e9b85b38c51db450efe Mon Sep 17 00:00:00 2001 From: Rubens Mariuzzo Date: Sat, 1 Oct 2016 12:56:24 -0400 Subject: [PATCH] Added event triggering. Any calls to `check`, `uncheck`, `toggle` and `range` will trigger `change` events to affected checkboxes. --- dist/jquery.checkboxes-1.1.0.js | 8 ++++---- src/jquery.checkboxes.js | 12 ++++++++---- tests/specs/jquery_checkboxes_check_spec.js | 4 ++++ tests/specs/jquery_checkboxes_toggle_spec.js | 4 ++++ tests/specs/jquery_checkboxes_uncheck_spec.js | 4 ++++ 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/dist/jquery.checkboxes-1.1.0.js b/dist/jquery.checkboxes-1.1.0.js index 183fd7d..439e191 100644 --- a/dist/jquery.checkboxes-1.1.0.js +++ b/dist/jquery.checkboxes-1.1.0.js @@ -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'); } /** @@ -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'); } /** @@ -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'); } /** @@ -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; }); diff --git a/src/jquery.checkboxes.js b/src/jquery.checkboxes.js index 6b9dc8c..f2b5856 100644 --- a/src/jquery.checkboxes.js +++ b/src/jquery.checkboxes.js @@ -23,7 +23,8 @@ this.$context.find(':checkbox') .filter(':not(:disabled)') .filter(':visible') - .prop('checked', true); + .prop('checked', true) + .trigger('change'); } /** @@ -32,7 +33,8 @@ uncheck() { this.$context.find(':checkbox:visible') .filter(':not(:disabled)') - .prop('checked', false); + .prop('checked', false) + .trigger('change'); } /** @@ -44,7 +46,8 @@ .each((i, element) => { let $checkbox = $(element); $checkbox.prop('checked', !$checkbox.is(':checked')); - }); + }) + .trigger('change'); } /** @@ -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; }); diff --git a/tests/specs/jquery_checkboxes_check_spec.js b/tests/specs/jquery_checkboxes_check_spec.js index cceb46a..da5c1df 100644 --- a/tests/specs/jquery_checkboxes_check_spec.js +++ b/tests/specs/jquery_checkboxes_check_spec.js @@ -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'); @@ -38,6 +40,8 @@ describe('The `check` method', function () { expect(modifiedState).toBe(originalState); } }); + + expect(spyEvent).toHaveBeenTriggered(); }); }); diff --git a/tests/specs/jquery_checkboxes_toggle_spec.js b/tests/specs/jquery_checkboxes_toggle_spec.js index eccb0da..3d251a4 100644 --- a/tests/specs/jquery_checkboxes_toggle_spec.js +++ b/tests/specs/jquery_checkboxes_toggle_spec.js @@ -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'); @@ -38,6 +40,8 @@ describe('The `toggle` method', function () { expect(modifiedState).toBe(originalState); } }); + + expect(spyEvent).toHaveBeenTriggered(); }); }); diff --git a/tests/specs/jquery_checkboxes_uncheck_spec.js b/tests/specs/jquery_checkboxes_uncheck_spec.js index 92c4b44..8e2db0e 100644 --- a/tests/specs/jquery_checkboxes_uncheck_spec.js +++ b/tests/specs/jquery_checkboxes_uncheck_spec.js @@ -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'); @@ -38,6 +40,8 @@ describe('The `uncheck` method', function () { expect(modifiedState).toBe(originalState); } }); + + expect(spyEvent).toHaveBeenTriggered(); }); });