我写jQuery插件的方式正确吗? [英] Is my way for writing jquery plugin correct?
问题描述
我已经开始编写jquery插件了,我想要:
当我像这样调用它时初始化它$(''selector'').sitemap(options);
为了能够在插件的功能中使用某些成员(例如"loader","viewPort")
关于第1条:我是否以编写初始化(init函数)的方式正确完成了此操作,或者还有其他更正确/更优雅的方法?
第2条:为了使用"loader","viewPort"之类的成员,我在sitemap对象中编写了所有函数.我做对了还是有更正确的\优雅的方法呢?
如果您可以建议我,我将非常高兴.
I''ve started writing jquery plugin and I want:
To initialize it when I call it like this $(''selector'').sitemap(options);
To be able using some members(like ''loader'', ''viewPort'') in functions in plugin
Regard 1st: have I done it correctly in the way I wrote initialization(init function) or there is more correct\elegant way to do it?
Regard 2nd: in order to use members like ''loader'', ''viewPort'' I wrote all functions in sitemap object. have I done it right or there is more correct\elegant way to do it?
I''ll be glad if you could suggest me Thank you very much
(function ($) {
$.extend($.fn, {
sitemap: function (options) {
//check if applied on valid DIV element
var canvas = this;
if (!canvas.is(''div'')) return;
var viewPort = null;
var loader = $(''<p id="initLoader">Loading...</p>'');
init();
loadMap();
function init() {
//create viewPort div
setCanvas();
}
function setCanvas() {
//set height and width
}
function loadMap() {
viewPort.prepend(loader);
buildMap($.parseJSON(''{"pages":[]}''));
}
function buildMap(map){
//...
}
})(jQuery);
谢谢
Thank you
推荐答案
(``selector'').sitemap(options);
为了能够在插件的功能中使用某些成员(例如"loader","viewPort")
关于第1条:我是否以编写初始化(init函数)的方式正确完成了此操作,或者还有其他更正确/更优雅的方法?
第2条:为了使用"loader","viewPort"之类的成员,我在sitemap对象中编写了所有函数.我做对了还是有更正确的\优雅的方法呢?
如果您可以建议我,我将非常高兴.
(''selector'').sitemap(options);
To be able using some members(like ''loader'', ''viewPort'') in functions in plugin
Regard 1st: have I done it correctly in the way I wrote initialization(init function) or there is more correct\elegant way to do it?
Regard 2nd: in order to use members like ''loader'', ''viewPort'' I wrote all functions in sitemap object. have I done it right or there is more correct\elegant way to do it?
I''ll be glad if you could suggest me Thank you very much
(function (
){
.extend(
这篇关于我写jQuery插件的方式正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!