-
-
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.
Merge pull request #16690 from simonihmig/rfc294-jquery-dot-event
Deprecate accessing jQuery.Event#originalEvent
- Loading branch information
Showing
3 changed files
with
216 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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
/* global Proxy */ | ||
import { deprecate } from '@ember/debug'; | ||
import { ENV } from 'ember-environment'; | ||
import { HAS_NATIVE_PROXY } from 'ember-utils'; | ||
import { DEBUG } from '@glimmer/env'; | ||
|
||
export default function addJQueryEventDeprecation(jqEvent) { | ||
if (!DEBUG || !HAS_NATIVE_PROXY) { | ||
return jqEvent; | ||
} | ||
|
||
let boundFunctions = new Map(); | ||
|
||
// 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 | ||
return new Proxy(jqEvent, { | ||
get(target, name) { | ||
switch (name) { | ||
case 'originalEvent': | ||
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', | ||
} | ||
); | ||
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') { | ||
// cache functions for reuse | ||
if (!boundFunctions.has(name)) { | ||
// for jQuery.Event methods call them with `target` as the `this` context, so they will access | ||
// `originalEvent` from the original jQuery event, not our proxy, thus not trigger the deprecation | ||
boundFunctions.set(name, target[name].bind(target)); | ||
} | ||
|
||
return boundFunctions.get(name); | ||
} | ||
// same for jQuery's getter functions for simple properties | ||
return target[name]; | ||
} | ||
}, | ||
}); | ||
} |