Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery UI: How to call a widget function with its namespace

Tags:

I created two custom JQuery UI widgets with same name but with different namespaces as given below: First widget:

$.widget('finance.dialog',{....}); // this was created in the file jquery.finance.dialog.js

Second widget:

$.widget('hr.dialog',{.....}); // this was created in the file jquery.hr.dialog.js

Apart from these two, JQuery UI has its own dialog widget (ui.dialog) in the namespace ui.

My question is: Which dialog widget will be called when i call the following in a web page as given below?

$('div#something').dialog(); 

Please note I include all the three widget variants in the web page.

I understand there are conflicts in the above scenario. How can we invoke a widget function with its namespace so that there will not be any conflicts?

like image 466
Vijey Avatar asked Oct 12 '11 11:10

Vijey


People also ask

What is a jQuery ui widget?

a jQuery UI widget is a specialized jQuery plug-in. Using plug-in, we can apply behaviours to the elements. However, plug-ins lack some built-in capabilities, such as a way to associate data with its elements, expose methods, merge options with defaults, and control the plug-in's lifetime.

What is a factory widget?

The widget factory defines how to create and destroy widgets, get and set options, invoke methods, and listen to events triggered by the widget. By using the widget factory to build your stateful plugins, you are automatically conforming to a defined standard, making it easier for new users to start using your plugins.

How do you extend widgets?

To allow for extension, $. widget() optionally accepts the constructor of a widget to use as a parent. When specifying a parent widget, pass it as the second argument - after the widget's name, and before the widget's prototype object.

How do I call a jQuery widget function?

Under the hoods, every instance of every widget is stored on the element using jQuery. data() . To retrieve the instance object, call jQuery. data() using the widget's full name as the key.


2 Answers

I had the same question, but I don't think it's possible to invoke a jquery ui widget with the namespace.

If I understand correctly from this: http://bugs.jqueryui.com/ticket/7489 By defining widgets with the same name, it's by design to overwrite earlier definitions. Because regardless of namespace, the widgets are mapped with their name to $.fn.

As suggested in the bug ticket you can use the bridge function to create a unique mapping to the specific namespaced widget and call it using the unique name.

In your case, it can be like this:

$.widget.bridge( "finance_dialog", $.finance.dialog );
$.widget.bridge( "hr_dialog", $.hr.dialog );

// then call it with...
$( "div#something" ).hr_dialog(); 

I suppose another way would be to create unique widget names in the first place.

like image 81
gary Avatar answered Sep 26 '22 03:09

gary


You can invoke a jQuery UI widget by its namespace like this:

$.ui.dialog(null, $('div#something'));  // Default jQ UI dialog
$.finance.dialog(null, $('div#something'));  // Your first custom dialog
$.hr.dialog(null, $('div#something'));  // Your second custom dialog

Use the first parameter, which is null in the example above, to send any options to the widget.

like image 44
Grigur Avatar answered Sep 27 '22 03:09

Grigur