-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate accessing jQuery.Event#originalEvent
Implements the deprecation message for user-land code accessing `originalEvent` on `jQuery.Event` instances, as proposed by RFC#294 (https://github.com/emberjs/rfcs/blob/master/text/0294-optional-jquery.md#introducing-ember-jquery-legacy-and-deprecating-jqueryevent-usage)
- Loading branch information
1 parent
236dd24
commit 707e85a
Showing
3 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/ember-views/lib/system/jquery_event_deprecation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { deprecate } from '@ember/debug'; | ||
import { ENV } from 'ember-environment'; | ||
import { HAS_NATIVE_PROXY } from 'ember-utils'; | ||
|
||
function deprecateJQueryEvent() { | ||
deprecate( | ||
'Accessing jQuery.Event specific properties is deprecated. Either use the ember-jquery-legacy addon to normalize events to native events, or explicitly opt into jQuery integration using @ember/optional-features.', | ||
ENV._JQUERY_INTEGRATION === true, | ||
{ | ||
id: 'ember-views.event-dispatcher.jquery-event', | ||
until: '4.0.0', | ||
} | ||
); | ||
} | ||
|
||
export default function addJQueryEventDeprecation(jqEvent) { | ||
if (!HAS_NATIVE_PROXY) { | ||
return jqEvent; | ||
} | ||
|
||
// wrap the jQuery event in a Proxy to add the deprecation message for originalEvent, according to RFC#294 | ||
// we need a native Proxy here, so we can make sure that the internal use of originalEvent in jQuery itself does | ||
// not trigger a deprecation | ||
/* global Proxy */ | ||
return new Proxy(jqEvent, { | ||
get(target, name) { | ||
switch (name) { | ||
case 'originalEvent': | ||
deprecateJQueryEvent(); | ||
return target[name]; | ||
|
||
// provide an escape hatch for ember-jquery-legacy to access originalEvent without a deprecation | ||
case '__originalEvent': | ||
return target.originalEvent; | ||
|
||
default: | ||
if (typeof target[name] === 'function') { | ||
// for methods jQuery.Event call them with `target` as the `this` context, so they will use access | ||
// `originalEvent` from the original jQuery event, not our proxy, thus not trigger the deprecation | ||
return target[name].bind(target); | ||
} | ||
// same for jQuery's getter functions for simple properties | ||
return target[name]; | ||
} | ||
}, | ||
}); | ||
} |