From 3a7aba911e900a117e78f1b40d2f6a03947147c7 Mon Sep 17 00:00:00 2001 From: Stephen James Date: Mon, 6 Jul 2015 10:57:36 -0400 Subject: [PATCH 1/2] Placard events: add namespace, make past tense --- index.js | 7 +++++++ js/placard.js | 21 ++++++++++++--------- test/placard-test.js | 10 +++++----- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index a6e27d03f..51522a480 100644 --- a/index.js +++ b/index.js @@ -440,6 +440,13 @@ define(function (require) { }); }); + $('#myPlacard3').on('accepted.fu.placard', function() { + console.log('accepted.fu.placard'); + }); + + $('#myPlacard3').on('cancelled.fu.placard', function() { + console.log('cancelled.fu.placard'); + }); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - RADIO diff --git a/js/placard.js b/js/placard.js index da510ce0e..98cf5ffd9 100644 --- a/js/placard.js +++ b/js/placard.js @@ -33,6 +33,8 @@ this.$element = $(element); this.options = $.extend({}, $.fn.placard.defaults, options); + this.EVENT_CALLBACK_MAP = { 'accepted': 'onAccept', 'cancelled': 'onCancel' }; + this.$accept = this.$element.find('.placard-accept'); this.$cancel = this.$element.find('.placard-cancel'); this.$field = this.$element.find('.placard-field'); @@ -51,9 +53,9 @@ this.$field.on('focus.fu.placard', $.proxy(this.show, this)); this.$field.on('keydown.fu.placard', $.proxy(this.keyComplete, this)); - this.$accept.on('click.fu.placard', $.proxy(this.complete, this, 'accept')); + this.$accept.on('click.fu.placard', $.proxy(this.complete, this, 'accepted')); this.$cancel.on('click.fu.placard', function (e) { - e.preventDefault(); self.complete('cancel'); + e.preventDefault(); self.complete('cancelled'); }); this.ellipsis(); @@ -63,30 +65,31 @@ constructor: Placard, complete: function (action) { - var func = this.options['on' + action[0].toUpperCase() + action.substring(1)]; + var func = this.options[ this.EVENT_CALLBACK_MAP[action] ]; + var obj = { previousValue: this.previousValue, value: this.$field.val() }; if (func) { func(obj); - this.$element.trigger(action, obj); + this.$element.trigger(action + '.fu.placard', obj); } else { - if (action === 'cancel' && this.options.revertOnCancel) { + if (action === 'cancelled' && this.options.revertOnCancel) { this.$field.val(this.previousValue); } - this.$element.trigger(action, obj); + this.$element.trigger(action + '.fu.placard', obj); this.hide(); } }, keyComplete: function (e) { if (this.isInput && e.keyCode === 13) { - this.complete('accept'); + this.complete('accepted'); this.$field.blur(); } else if (e.keyCode === 27) { - this.complete('cancel'); + this.complete('cancelled'); this.$field.blur(); } }, @@ -261,7 +264,7 @@ $.fn.placard.defaults = { onAccept: undefined, onCancel: undefined, - externalClickAction: 'cancel', + externalClickAction: 'cancelled', externalClickExceptions: [], explicit: false, revertOnCancel: -1//negative 1 will check for an '.placard-accept' button. Also can be set to true or false diff --git a/test/placard-test.js b/test/placard-test.js index 825fb2f3e..791e9948e 100644 --- a/test/placard-test.js +++ b/test/placard-test.js @@ -27,7 +27,7 @@ define(function(require){ $('body').append($placard); $placard.placard(); - $placard.on('cancel', function(e, helpers){ + $placard.on('cancelled.fu.placard', function(e, helpers){ ok(1===1, 'default action event (cancel) triggered upon external click'); }); @@ -43,7 +43,7 @@ define(function(require){ var $placard = $(html).find('#placard2'); $placard.placard(); - $placard.on('cancel', function(e, helpers){ + $placard.on('cancelled.fu.placard', function(e, helpers){ ok(1===1, 'default action event (cancel) triggered upon external click'); }); $('body').append($placard); @@ -71,13 +71,13 @@ define(function(require){ var $placard = $(html).find('#placard1'); $placard.placard(); - $placard.on('accept', function(e, helpers){ + $placard.on('accepted.fu.placard', function(e, helpers){ ok(1===1, 'accept event triggers on accept'); equal(typeof e, 'object', 'event object passed in accept event'); equal(typeof helpers, 'object', 'helpers object passed in accept event'); equal((helpers.previousValue!==undefined && helpers.value!==undefined), true, 'helpers object contains correct attributes'); }); - $placard.on('cancel', function(e, helpers){ + $placard.on('cancelled.fu.placard', function(e, helpers){ ok(1===1, 'cancel event triggers on cancel'); equal(typeof e, 'object', 'event object passed in cancel event'); equal(typeof helpers, 'object', 'helpers object passed in cancel event'); @@ -146,7 +146,7 @@ define(function(require){ var $placard = $(html).find('#placard1'); $placard.placard({ - externalClickAction: 'accept' + externalClickAction: 'accepted' }); $placard.find('input').focus().focus().val('test'); From 12fac35937fc59a9c4652b27e847a6891287e69c Mon Sep 17 00:00:00 2001 From: Stephen James Date: Thu, 9 Jul 2015 13:15:51 -0400 Subject: [PATCH 2/2] Make placard's EVENT_CALLBACK_MAP private --- js/placard.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/js/placard.js b/js/placard.js index 98cf5ffd9..b8296700d 100644 --- a/js/placard.js +++ b/js/placard.js @@ -25,6 +25,7 @@ // -- BEGIN MODULE CODE HERE -- var old = $.fn.placard; + var EVENT_CALLBACK_MAP = { 'accepted': 'onAccept', 'cancelled': 'onCancel' }; // PLACARD CONSTRUCTOR AND PROTOTYPE @@ -33,8 +34,6 @@ this.$element = $(element); this.options = $.extend({}, $.fn.placard.defaults, options); - this.EVENT_CALLBACK_MAP = { 'accepted': 'onAccept', 'cancelled': 'onCancel' }; - this.$accept = this.$element.find('.placard-accept'); this.$cancel = this.$element.find('.placard-cancel'); this.$field = this.$element.find('.placard-field'); @@ -65,7 +64,7 @@ constructor: Placard, complete: function (action) { - var func = this.options[ this.EVENT_CALLBACK_MAP[action] ]; + var func = this.options[ EVENT_CALLBACK_MAP[action] ]; var obj = { previousValue: this.previousValue,