扩展jQueryUI小部件而不会发生冲突

扩展jQueryUI小部件而不会发生冲突

本文介绍了扩展jQueryUI小部件而不会发生冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣扩展jqueryUI对话框小部件的行为.在过去,我重写了这样的函数:

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

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

但是我认为我应该改为使用小部件工厂来创建一个新的小部件,而不是从对话框继承.

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

我相信代码应如下所示:

I believe the code should look like this:

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

我的问题是,即使我的对话框窗口小部件位于cs命名空间中,而jquery的ui命名空间中,这也会引起冲突吗?我相信jQuery会将widget方法附加到$ .fn,所以我认为可以.

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

如果是这样,如果仍然遇到冲突,命名空间的目的是什么?我是否需要将其小部件命名为"cs.csDialog"以保持其唯一性?我觉得我对此有所了解.

If so, what's the purpose of the namespace if you still get conflicts? DO I need to name my widget "cs.csDialog" to keep it unique? I feel like there's something missing in my understanding of this.

感谢您的帮助或澄清.

推荐答案

$.widget()在内部调用$.widget.bridge(),将小部件方法添加到$对象:

$.widget() calls $.widget.bridge() internally to add the widget method to the $ object:

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

如您所见,它仅传递窗口小部件的名称和创建函数.命名空间仅用于访问创建函数,而不是方法名称的一部分.

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

因此,在您的情况下,调用小部件dialog确实会替换$.dialog().但是,原始的$.dialog()方法将保持为$.ui.dialog()可用,并且以同样的方式,您的方法将始终以$.cs.dialog()可用.

So, in your case, calling your widget dialog will indeed replace $.dialog(). The original $.dialog() method will, however, remain available as $.ui.dialog() and, in the same way, yours will always be available as $.cs.dialog().

这篇关于扩展jQueryUI小部件而不会发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:08