From c04aee63d4f5f8dd0ff4dd92b8289c3673cb9b1b Mon Sep 17 00:00:00 2001 From: Mike Nelson Date: Thu, 18 Sep 2014 09:54:32 -0400 Subject: [PATCH] allow data to be provided when trigger is invoked --- build/debug/cash.js | 7 +++++-- spec/event.spec.js | 23 ++++++++++++++++------- src/events.js | 7 +++++-- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/build/debug/cash.js b/build/debug/cash.js index 606cf05..c5cbc13 100644 --- a/build/debug/cash.js +++ b/build/debug/cash.js @@ -207,7 +207,7 @@ cash.on = function(type, fn, sel, data, cap) { // pass the namespace along to the listener if(ns) e.namespace = ns; // pass any custom data along to the listener - if(data) e.data = data; + e.data = $.extend({}, data || {}, e.data); // base case is that this is not 'delegated' if(!sel) fn.call(el, e); // there is a sel, check for matches and call if so. @@ -237,10 +237,13 @@ cash.on = function(type, fn, sel, data, cap) { // // `param` {string} `e` // +// `param` {object} `data` optional hash which is passed along with the event as event.data +// // `returns` cash -cash.trigger = function(e) { +cash.trigger = function(e, data) { var evt = document.createEvent('Event'); evt.initEvent(e, true, true); + evt.data = data; this.q.forEach(function(el) {el.dispatchEvent && el.dispatchEvent(evt);}); return this; }; diff --git a/spec/event.spec.js b/spec/event.spec.js index 2d58e02..dc9e17d 100644 --- a/spec/event.spec.js +++ b/spec/event.spec.js @@ -68,14 +68,23 @@ describe('Event', function() { }); it('passes custom data with the event', function() { - var spy = spyOn(window, 'callBackWithData').andCallThrough(); + var spy = spyOn(window, 'callBackWithData'); $(document.querySelector('#testTarget')).on('click', window.callBackWithData, null, {custom: 'soCustom'}).trigger('click').off('click'); expect(spy).toHaveBeenCalled(); + expect(spy.calls[0].args[0].data).toEqual({custom: 'soCustom'}); $(document.querySelector('#testTarget')).on('click', window.callBackWithData, 'div:last-child', {custom: 'soSoCustom'}).find('div:last-child').trigger('click'); $(document.querySelector('#testTarget')).off('click'); - expect(spy).toHaveBeenCalled(); + expect(spy.calls[1].args[0].data).toEqual({custom: 'soSoCustom'}); + }); + + it('allows data to be passed with trigger and extends the custom data', function() { + var spy = spyOn(window, 'callBackWithData'); + $(document.querySelector('#testTarget')).on('click', window.callBackWithData, null, + {custom: 'soCustom'}).trigger('click', {custom: 'wildlyCustom'}).off('click'); + var evt = spy.calls[0].args[0]; + expect(evt.data).toEqual({custom: 'wildlyCustom'}); }); it('binds, unbinds and honors namespaced events', function() { @@ -161,12 +170,12 @@ describe('Event', function() { expect(target.id).toEqual(li2.id); }); - + it('on can be insructed to use the capture phase for on and off', function() { var tt = document.querySelector('#testTarget'), ary = []; window.meFirst = function(e) {ary.push('capture');}; window.meSecond = function(e) {ary.push('bubble');}; - + $(tt).on('click', window.meFirst, null, null, true).on('click', window.meSecond); expect($.cache.events[tt.getAttribute('cid')].click[0].cap).toBe(true); $.trigger('click'); @@ -176,13 +185,13 @@ describe('Event', function() { $.off('click', window.meFirst).trigger('click'); expect(ary[2]).toBe('capture'); expect(ary[3]).toBe('bubble'); - + $.off('click', window.meFirst, true).trigger('click'); expect(ary[4]).toBe('bubble'); $.off('click', window.meSecond).trigger('click'); - expect(ary.length).toBe(5); + expect(ary.length).toBe(5); }); - + it('will force capture on focus and blur if delegated', function() { window.focused = 0; window.blurred = 0; diff --git a/src/events.js b/src/events.js index 1b76d5e..c1b14ff 100644 --- a/src/events.js +++ b/src/events.js @@ -68,7 +68,7 @@ cash.on = function(type, fn, sel, data, cap) { // pass the namespace along to the listener if(ns) e.namespace = ns; // pass any custom data along to the listener - if(data) e.data = data; + e.data = $.extend({}, data || {}, e.data); // base case is that this is not 'delegated' if(!sel) fn.call(el, e); // there is a sel, check for matches and call if so. @@ -98,10 +98,13 @@ cash.on = function(type, fn, sel, data, cap) { // // `param` {string} `e` // +// `param` {object} `data` optional hash which is passed along with the event as event.data +// // `returns` cash -cash.trigger = function(e) { +cash.trigger = function(e, data) { var evt = document.createEvent('Event'); evt.initEvent(e, true, true); + evt.data = data; this.q.forEach(function(el) {el.dispatchEvent && el.dispatchEvent(evt);}); return this; };