Since the plugin returns this keyword, it maintains chainability so that elements collected by jQuery can continue to be controlled by jQuery methods such as .css. Therefore, if your plugin does not return an intrinsic value, you should always return the this keyword within its scope. Additionally, you might deduce that parameters passed to a plugin will be passed within the plugin's scope. Therefore, in the previous example, the string 'width' becomes a type parameter of the plugin.
This is a very flexible way to provide a highly configurable plugin without requiring the developer to define all available options.
6. Namespace
Properly naming your plugin is a very important part of plugin development. With the right namespace, you can guarantee that your plugin will have a very low chance of being overwritten by other plugins or other code on the same page. Namespaces also make your life as a plugin developer easier because it helps you better keep track of your methods, events, and data.
Under no circumstances should a single plugin have multiple namespaces within the jQuery.fnjQuery.fn object.
};
$.fn.tooltipUpdate = function (content) {
// !!!
};
})(jQuery);
This is discouraged because $.fn clutters the $.fn namespace. To solve this problem, you should collect all the plugin's methods in the object text and call them by passing the string name of the method to the plugin.
(function ($) {
var methods = {
init: function (options) {
},
show: function () // is
},
hide: Function () {
// Good
},
Update: Function (Content) {
// !!!
}
};
$. fn.tooltip = function (method) {
// Method call (arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments); 🎜> >//Call the init method
$('div').tooltip();
//Call the init method
$('div').tooltip({
foo: ' bar'
});
//Call the hide method
$('div').tooltip('hide');
//Call the Update method
$('div').tooltip('update', 'This is the new tooltip content!');
This type of plugin architecture allows you to encapsulate all methods in a parent package and call them by passing the method's string name and additional parameters required by this method. This type of encapsulation and architecture is standard in the jQuery plug-in community, and it is used by countless plug-ins, including plug-ins and widgets in jQuery UI.
8. Events
A little-known function of the bind method allows binding event namespaces. If your plugin binds an event, a good practice is to namespace this event. This way, when you unbind, you won't interfere with other events of the same type that may already be bound. You can do this by appending the namespace to the event you need to bind via '.'.
Copy code
The code is as follows:
(function ($) {
var methods = {
init: function (options) {
return this.each(function () {
$(window).bind('resize.tooltip', methods.reposition);
});
},
destroy: function () {
return this.each(function () {
$(window).unbind('.tooltip');
})
},
reposition: function () {
//...
},
show: function () {
//...
},
hide: function () {
//...
},
update: function (content) {
//...
}
};
$.fn.tooltip = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' method ' does not exist on jQuery.tooltip');
}
};
})(jQuery);
$('#fun').tooltip();
//一段时间之后... ...
$('#fun').tooltip('destroy');
在这个例子中,当tooltip通过init方法初始化时,它将reposition方法绑定到resize事件并给reposition非那方法赋予命名空间通过追加.tooltip。 稍后, 当开发人员需要销毁tooltip的时候,我们可以同时解除其中reposition方法和resize事件的绑定,通过传递reposition的命名空间给插件。 这使我们能够安全地解除事件的绑定并不会影响到此插件之外的绑定。
九、数据
通常在插件开发的时候,你可能需要记录或者检查你的插件是否已经被初始化给了一个元素。 使用jQuery的data方法是一个很好的基于元素的记录变量的途径。尽管如此,相对于记录大量的不同名字的分离的data, 使用一个单独的对象保存所有变量,并通过一个单独的命名空间读取这个对象不失为一个更好的方法。
(function ($) {
var methods = {
init: function (options) {
return this.each(function () {
var $this = $(this),
data = $this.data('tooltip'),
tooltip = $('
', {
text: $this.attr('title')
});
// If the plugin hasn't been initialized yet
if (!data) {
/*
Do more setup stuff here
*/
$(this).data('tooltip', {
target: $this,
tooltip: tooltip
});
}
});
},
destroy: function () {
return this.each(function () {
var $this = $(this),
data = $this.data('tooltip');
// Namespacing FTW
$(window).unbind('.tooltip');
data.tooltip.remove();
$this.removeData('tooltip');
})
},
reposition: function () {
// ...
},
show: function () {
// ...
},
hide: function () {
// ...
},
update: function (content) {
// ...
}
};
$.fn.tooltip = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}else {
$.error('Method ' method ' does not exist on jQuery.tooltip'); >
Encapsulating data in an object through a namespace makes it easier to read all plugin properties from a centralized location.
10. Summary and best practices
Writing jQuery plug-ins allows you to make libraries that integrate the most useful functions into reusable code, saving developers time and making development more efficient. When developing jQuery plugins, keep in mind:
1. Always wrapped in a closed plug-in:
Copy code
3. Unless The plugin returns a specific value, otherwise the this keyword is always returned to maintain chainability.
4. Pass an expandable default object parameter instead of a large number of parameters to the plug-in.
5. Do not name different methods multiple times in a plug-in.
3. Always namespace methods, events and data.