Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow data to be provided when trigger is invoked #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions build/debug/cash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a case here where we don't call extend? Also this would possibly leave us with a e.data = {} -- not sure if we care there or not, but previously e.data would only be present if it had stuff

// 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.
Expand Down Expand Up @@ -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;
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with this

Expand Down
23 changes: 16 additions & 7 deletions spec/event.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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');
Expand All @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
};