Extending jQueryUI widget without conflicts

I'm interested in extending the jqueryUI dialog widget behavior. I've overridden functions like this in the past:

//Custom dragging
$.ui.dialog.prototype._makeDraggable = function () {}

      

But I think I should switch to using a factory widget to create a new widget that inherits from the dialog.

I believe the code should look like this:

(function($, undefined) {
  $.widget('cs.dialog', $.ui.dialog, {
    // definition of the widget goes here
  });
}(jQuery));

      

My question is, would this cause a conflict even though my dialog widget is in the cs namespace and jQuery is in the ui namespace? I believe jquery binds the widget method to $ .fn, so I think it will.

If so, what is the purpose of the namespace if you still have conflicts? Do I need to name my widget "cs.csDialog" to be unique? I feel that something is missing in my understanding.

Thanks for any help or clarification.

+2


source to share


1 answer


$.widget()

calls $.widget.bridge()

internally to add a widget method to an object $

:

$.widget.bridge(name, $[namespace][name]);

      



As you can see, it only passes the widget name and create function. The namespace is only used to access the create function and is not part of the method name.

So, in your case, calling your widget dialog

will indeed replace $.dialog()

. However, the original method $.dialog()

remains available as $.ui.dialog()

, and, in the same way, your number will always be available as $.cs.dialog()

.

+2


source







All Articles