Skip to content

Make events work in jQuery no-conflict scopes [fixes #350] #352

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

Merged
merged 2 commits into from
Mar 23, 2016
Merged
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
6 changes: 3 additions & 3 deletions src/lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

'use strict';

/* global $:false */
/* global jQuery:false */

var EventEmitter = require('events').EventEmitter;

Expand Down Expand Up @@ -54,7 +54,7 @@ var Events = {
*/
plotObj.emit = function(event, data) {
if(typeof jQuery !== 'undefined') {
$(plotObj).trigger(event, data);
jQuery(plotObj).trigger(event, data);
}

ev.emit(event, data);
Expand All @@ -77,7 +77,7 @@ var Events = {
* collect the return value of the LAST handler function
*/
if(typeof jQuery !== 'undefined') {
jQueryHandlerValue = $(plotObj).triggerHandler(event, data);
jQueryHandlerValue = jQuery(plotObj).triggerHandler(event, data);
}

/*
Expand Down
107 changes: 104 additions & 3 deletions test/jasmine/tests/events_test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* global $:false */
/* global $:false, jQuery:false */

/*
* Note this test requires JQuery in the global scope.
* we should keep it that way to keep testing our backward
* compatibility with JQuery events.
*/


var Events = require('@src/lib/events');

describe('Events', function() {
Expand Down Expand Up @@ -62,7 +61,6 @@ describe('Events', function() {
it('triggers jquery events', function(done) {
Events.init(plotDiv);


$(plotDiv).bind('ping', function(event, data) {
expect(data).toBe('pong');
done();
Expand Down Expand Up @@ -192,4 +190,107 @@ describe('Events', function() {
});
});

describe('when jQuery.noConflict is set, ', function() {

beforeEach(function() {
$.noConflict();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

});

afterEach(function() {
window.$ = jQuery;
});

it('triggers jquery events', function(done) {

Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function(event, data) {
expect(data).toBe('pong');
done();
});

setTimeout(function() {
jQuery(plotDiv).trigger('ping', 'pong');
});
});

it('triggers jQuery handlers when no matching node events bound', function() {
var eventBaton = 0;

Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

/*
* This will not be called
*/
plotDiv.on('pong', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(2);
expect(result).toBe('pong');
});

it('triggers jQuery handlers when no node events initialized', function() {
var eventBaton = 0;

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(3);
expect(result).toBe('pong');
});

it('triggers jQuery + nodejs handlers and returns last jQuery value', function() {
var eventBaton = 0;

Events.init(plotDiv);

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'ping';
});

plotDiv.on('ping', function() {
eventBaton++;
return 'ping';
});

jQuery(plotDiv).bind('ping', function() {
eventBaton++;
return 'pong';
});

var result = Events.triggerHandler(plotDiv, 'ping');

expect(eventBaton).toBe(3);
expect(result).toBe('pong');
});
});
});