Skip to content

Commit 4ba05d8

Browse files
committed
Merge pull request #352 from plotly/fix-jQuery-noConflict
Make events work in jQuery no-conflict scopes [fixes #350]
2 parents 165a1aa + 2a77b03 commit 4ba05d8

File tree

2 files changed

+107
-6
lines changed

2 files changed

+107
-6
lines changed

src/lib/events.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
'use strict';
1111

12-
/* global $:false */
12+
/* global jQuery:false */
1313

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

@@ -54,7 +54,7 @@ var Events = {
5454
*/
5555
plotObj.emit = function(event, data) {
5656
if(typeof jQuery !== 'undefined') {
57-
$(plotObj).trigger(event, data);
57+
jQuery(plotObj).trigger(event, data);
5858
}
5959

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

8383
/*

test/jasmine/tests/events_test.js

+104-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
/* global $:false */
1+
/* global $:false, jQuery:false */
22

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

9-
109
var Events = require('@src/lib/events');
1110

1211
describe('Events', function() {
@@ -62,7 +61,6 @@ describe('Events', function() {
6261
it('triggers jquery events', function(done) {
6362
Events.init(plotDiv);
6463

65-
6664
$(plotDiv).bind('ping', function(event, data) {
6765
expect(data).toBe('pong');
6866
done();
@@ -192,4 +190,107 @@ describe('Events', function() {
192190
});
193191
});
194192

193+
describe('when jQuery.noConflict is set, ', function() {
194+
195+
beforeEach(function() {
196+
$.noConflict();
197+
});
198+
199+
afterEach(function() {
200+
window.$ = jQuery;
201+
});
202+
203+
it('triggers jquery events', function(done) {
204+
205+
Events.init(plotDiv);
206+
207+
jQuery(plotDiv).bind('ping', function(event, data) {
208+
expect(data).toBe('pong');
209+
done();
210+
});
211+
212+
setTimeout(function() {
213+
jQuery(plotDiv).trigger('ping', 'pong');
214+
});
215+
});
216+
217+
it('triggers jQuery handlers when no matching node events bound', function() {
218+
var eventBaton = 0;
219+
220+
Events.init(plotDiv);
221+
222+
jQuery(plotDiv).bind('ping', function() {
223+
eventBaton++;
224+
return 'ping';
225+
});
226+
227+
/*
228+
* This will not be called
229+
*/
230+
plotDiv.on('pong', function() {
231+
eventBaton++;
232+
return 'ping';
233+
});
234+
235+
jQuery(plotDiv).bind('ping', function() {
236+
eventBaton++;
237+
return 'pong';
238+
});
239+
240+
var result = Events.triggerHandler(plotDiv, 'ping');
241+
242+
expect(eventBaton).toBe(2);
243+
expect(result).toBe('pong');
244+
});
245+
246+
it('triggers jQuery handlers when no node events initialized', function() {
247+
var eventBaton = 0;
248+
249+
jQuery(plotDiv).bind('ping', function() {
250+
eventBaton++;
251+
return 'ping';
252+
});
253+
254+
jQuery(plotDiv).bind('ping', function() {
255+
eventBaton++;
256+
return 'ping';
257+
});
258+
259+
jQuery(plotDiv).bind('ping', function() {
260+
eventBaton++;
261+
return 'pong';
262+
});
263+
264+
var result = Events.triggerHandler(plotDiv, 'ping');
265+
266+
expect(eventBaton).toBe(3);
267+
expect(result).toBe('pong');
268+
});
269+
270+
it('triggers jQuery + nodejs handlers and returns last jQuery value', function() {
271+
var eventBaton = 0;
272+
273+
Events.init(plotDiv);
274+
275+
jQuery(plotDiv).bind('ping', function() {
276+
eventBaton++;
277+
return 'ping';
278+
});
279+
280+
plotDiv.on('ping', function() {
281+
eventBaton++;
282+
return 'ping';
283+
});
284+
285+
jQuery(plotDiv).bind('ping', function() {
286+
eventBaton++;
287+
return 'pong';
288+
});
289+
290+
var result = Events.triggerHandler(plotDiv, 'ping');
291+
292+
expect(eventBaton).toBe(3);
293+
expect(result).toBe('pong');
294+
});
295+
});
195296
});

0 commit comments

Comments
 (0)