Skip to content

Commit

Permalink
Change preferJQueryEvents to useOnlyNativeEvents; fix and expand rela…
Browse files Browse the repository at this point in the history
…ted tests.
  • Loading branch information
mbest committed May 29, 2015
1 parent 846025a commit 09b9e93
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
62 changes: 46 additions & 16 deletions spec/utilsDomBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,59 @@ describe('setTextContent', function () {
});

describe('registerEventHandler', function() {
it ('should not use jQuery eventing with preferJQueryEvents option set to false', function() {
var jQueryLoaded = (typeof jQuery !== 'undefined');
it ('if jQuery is referenced, should use jQuery eventing with useOnlyNativeEvents option set to false', function() {
if (typeof jQuery === 'undefined') {
return; // Nothing to test. Run the specs with jQuery referenced for this to do anything.
}

this.restoreAfter(ko.options, 'useOnlyNativeEvents');

var element = document.createElement('DIV');
var eventFired = false;
var jQueryUsed = false;
var jQueryModified = false;

// Set the option to true.
ko.options.preferJQueryEvents = false;

// If jQuery is present, verify jQuery is not used in event binding.
if (jQueryLoaded) {
ko.utils.registerEventHandler(element, 'click', function(eventArgs) {
eventFired = true;
jQueryUsed = !!eventArgs.originalEvent;
});
}
ko.options.useOnlyNativeEvents = false;

// Trigger the event.
// Verify jQuery is used in event binding.
ko.utils.registerEventHandler(element, 'click', function(eventArgs) {
eventFired = true;
jQueryModified = !!eventArgs.originalEvent;
});

// Trigger the event natively (jQuery intercepts and creates new event object, which we can test)
element.click();
expect(eventFired && jQueryModified).toBe(true);

// Also trigger an event through ko.utils.triggerEvent to show that it creates a jQuery event directly
eventFired = jQueryModified = false;
ko.utils.triggerEvent(element, 'click');
expect(eventFired && !jQueryModified).toBe(true);
});

// Reset the option.
ko.options.preferJQueryEvents = true;
it ('should not use jQuery eventing with useOnlyNativeEvents option set to true', function() {
this.restoreAfter(ko.options, 'useOnlyNativeEvents');

expect(!jQueryLoaded || (eventFired && !jQueryUsed)).toBe(true);
var element = document.createElement('DIV');
var eventFired = false;
var jQueryModified = false;

// Set the option to true.
ko.options.useOnlyNativeEvents = true;

// Verify jQuery is not used in event binding.
ko.utils.registerEventHandler(element, 'click', function(eventArgs) {
eventFired = true;
jQueryModified = !!eventArgs.originalEvent;
});

// Trigger the event natively
element.click();
expect(eventFired && !jQueryModified).toBe(true);

// Also trigger an event through ko.utils.triggerEvent to show that it triggers a native event
eventFired = jQueryModified = false;
ko.utils.triggerEvent(element, 'click');
expect(eventFired && !jQueryModified).toBe(true);
});
});
2 changes: 1 addition & 1 deletion src/options.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// For any options that may affect various areas of Knockout and aren't directly associated with data binding.
ko.options = {
'deferUpdates': false,
'preferJQueryEvents': true
'useOnlyNativeEvents': false
};

//ko.exportSymbol('options', ko.options); // 'options' isn't minified
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ ko.utils = (function () {
var wrappedHandler = ko.utils.catchFunctionErrors(handler);

var mustUseAttachEvent = ieVersion && eventsThatMustBeRegisteredUsingAttachEvent[eventType];
if (ko.options['preferJQueryEvents'] && !mustUseAttachEvent && jQueryInstance) {
if (!ko.options['useOnlyNativeEvents'] && !mustUseAttachEvent && jQueryInstance) {
jQueryInstance(element)['bind'](eventType, wrappedHandler);
} else if (!mustUseAttachEvent && typeof element.addEventListener == "function")
element.addEventListener(eventType, wrappedHandler, false);
Expand Down Expand Up @@ -389,7 +389,7 @@ ko.utils = (function () {
// In both cases, we'll use the click method instead.
var useClickWorkaround = isClickOnCheckableElement(element, eventType);

if (ko.options['preferJQueryEvents'] && jQueryInstance && !useClickWorkaround) {
if (!ko.options['useOnlyNativeEvents'] && jQueryInstance && !useClickWorkaround) {
jQueryInstance(element)['trigger'](eventType);
} else if (typeof document.createEvent == "function") {
if (typeof element.dispatchEvent == "function") {
Expand Down

0 comments on commit 09b9e93

Please sign in to comment.