Skip to content
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

jQuery-UI widget breaks if using as a module #2015

Closed
MhMadHamster opened this issue Nov 14, 2021 · 4 comments · Fixed by #2019
Closed

jQuery-UI widget breaks if using as a module #2015

MhMadHamster opened this issue Nov 14, 2021 · 4 comments · Fixed by #2019

Comments

@MhMadHamster
Copy link

MhMadHamster commented Nov 14, 2021

Hello, currently in our project we are using jquery-ui widgets as a separate modules like this:

import dialog from "jquery-ui/ui/widgets/dialog";

dialog({ ...options }, document.querySelector(".some-element");

However in 1.13.0 we cant use widgets this way, now it fails to initialize a widget with an error:
Uncaught TypeError: Cannot read properties of undefined (reading '_createWidget')

Even though you still able to use widgets "normally":

import "jquery-ui/ui/widgets/dialog";

$(".some-element").dialog({ ...options });

would be nice to fix this issue, especially since it looks unintended.

I believe problem was introduced in this commit: 70dae67
By adding "use strict" now this code: https://github.com/jquery/jquery-ui/blob/1.13.0/ui/widget.js#L80 fails, since this is undefined.

@mgol
Copy link
Member

mgol commented Nov 15, 2021

Thanks for the report. I wonder what was the intention when this is not passed; perhaps to just evaluate that if contidion to a falsy value? Maybe we just need to check for this as well.

Planning the fix for 1.13.1.

@mgol
Copy link
Member

mgol commented Nov 18, 2021

The check causing an issue is:

		// Allow instantiation without "new" keyword
		if ( !this._createWidget ) {
			return new constructor( options, element );
		}

So, basically, this is a duck-typing check to make sure we've got a widget instance and not another object. In most cases it works even now but if you save the constructor to a variable and call it directly, no this will be set while previously it'd be the global object. When not used as a module, you'd call it via $.ui.dialog(...) so this would be $.ui.

Adding a check for this being truthy should be enough then, I'll submit a fix shortly.

For your case, before UI 1.13.1 is available, you can workaround the issue by changing your code to:

import dialog from "jquery-ui/ui/widgets/dialog";

dialog.call($.ui, { ...options }, document.querySelector(".some-element");

or:

import dialog from "jquery-ui/ui/widgets/dialog";

dialog.call(window, { ...options }, document.querySelector(".some-element");

Of course, I understand the feasibility of this depends on how many of these imports you have. 🙂

mgol added a commit to mgol/jquery-ui that referenced this issue Nov 18, 2021
Due to the fact the widget factory code is now in strict mode, the check for
being called without using the `new` keyword started breaking if you save the
widget constructor to a variable before calling it:
```js
var customWidget = $.custom.customWidget;
customWidget( {}, elem );
```
as then `this` is undefined and checking for `this._createWidget` crashes.
Account for that with an additional check.

Fixes jquerygh-2015
@mgol
Copy link
Member

mgol commented Nov 18, 2021

PR with a fix: #2019.

mgol added a commit that referenced this issue Nov 18, 2021
Due to the fact the widget factory code is now in strict mode, the check for
being called without using the `new` keyword started breaking if you save the
widget constructor to a variable before calling it:
```js
var customWidget = $.custom.customWidget;
customWidget( {}, elem );
```
as then `this` is undefined and checking for `this._createWidget` crashes.
Account for that with an additional check.

Fixes gh-2015
Closes gh-2019
@mgol
Copy link
Member

mgol commented Jan 20, 2022

jQuery UI 1.13.1 including a fix for this issue has been released: https://blog.jqueryui.com/2022/01/jquery-ui-1-13-1-released/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment