jQuery发展趋势一片大好,这里向大家介绍一种管理jQuery插件的方案。可能有些人已经在系统中已经使用.

使用原因:在开发过程中,jQuery插件的使用越来越多,且jQuery的某些插件是基于某些插件来使用了,且有先后顺序的问题.

最初的做法:直接在页面上加载js.如下代码,其中使用了一些插件,其依赖于jQuery.

其中用了一个jdMenu插件,其依赖于四个文件(jdMenu.css,jquery.js,bgiframe.min.js,positionBy.js)

Code<linkhref="/App_Scripts/jPlugin/jdMenu/jquery.jdMenu.css"rel="stylesheet"type="text/css"/><scriptsrc="/App_Scripts/jquery.js"type="text/javascript"></script><scriptsrc="/App_Scripts/jPlugin/jquery-bgiframe/jquery.bgiframe.min.js"type="text/javascript"></script><scriptsrc="/App_Scripts/jPlugin/jdMenu/jquery.positionBy.js"type="text/javascript"></script><scriptsrc="/App_Scripts/jPlugin/jdMenu/jquery.jdMenu.js"type="text/javascript"></script><scriptsrc="/App_Scripts/jPlugin/jquery-hint/hint.jquery.js"type="text/javascript"></script><scriptsrc="/App_Scripts/jPlugin/utilities/customExt.js"type="text/javascript"></script><scriptsrc="/App_Scripts/jPlugin/cookie/cookie.pack.js"type="text/javascript"></script><title>

上面用到的插件还不算多,在某些页面,我们曾经同时用到很多插件,如jQuery的Tab,autoComplete,validate及相关其他插件,这样插件相关的文件就很多很多了,随着开发的进行,越难越管理,而且jQuery也在不断的升级,不同的插件还会出现版本的问题.随着这些问题的出现,急需一个配置文件来配置管理.

一.定义jQuery资源配置文件

以下为我定义的基本格式

Code<?xml version="1.0" encoding="utf-8"?><Resources><Common><Plugins><PluginName="cookie"Src="~/App_Scripts/jPlugin/cookie/cookie.pack.js"><Styles></Styles><Scripts><ScriptKey="jquery"Src="~/App_Scripts/jquery.js"/></Scripts></Plugin><PluginName="treeview"Src="~/App_Scripts/jPlugin/jquery-treeview/jquery.treeview.pack.js"><Styles><StyleKey="jquery_treeview_css"Href="~/App_Scripts/jPlugin/jquery-treeview/jquery.treeview.css"/></Styles><Scripts><ScriptKey="jquery"Src="~/App_Scripts/jquery.js"/></Scripts></Plugin><PluginName="autocomplete"Src="~/App_Scripts/jPlugin/jquery-autocomplete/jquery.autocomplete-bundle.pack.js"><Styles><StyleKey="autocomplete_css"Href="~/App_Scripts/jPlugin/jquery-autocomplete/jquery.autocomplete.css"/></Styles><Scripts><ScriptKey="jquery"Src="~/App_Scripts/jquery.js"/><ScriptKey="bgiframe"Src="~/App_Scripts/jPlugin/jquery-bgiframe/jquery.bgiframe.min.js"/></Scripts></Plugin></Plugins></Common><Required><Scripts><ScriptName="jdMenu"></Script><ScriptName="hint"></Script><ScriptName="jQueryCustomExt"></Script><ScriptName="cookie"></Script></Scripts></Required><Pages><PageSrc="~/Administration/EmployeeMgmt.aspx"><Scripts><ScriptName="jTemplates"></Script><ScriptName="treeview"></Script><ScriptName="scrollTo"></Script><ScriptName="validate"></Script><ScriptName="jQueryCustomExt"></Script><ScriptName="autocomplete"></Script><ScriptName="blockUI"></Script></Scripts></Page><PageSrc="~/ScheduleMgmt/Reschedule.aspx"><Scripts><ScriptName="My97DatePicker"></Script><ScriptName="jQueryCustomExt"></Script><ScriptName="draggable_1.0"></Script><ScriptName="jTemplates"></Script><ScriptName="tab_1.0"></Script><ScriptName="autocomplete"></Script><ScriptName="spinBtn"></Script><ScriptName="hotkeys"></Script><ScriptName="datepicker"></Script><ScriptName="clockpick"></Script><ScriptName="validate"></Script><ScriptName="blockUI"></Script><ScriptName="tooltip"></Script><ScriptName="contextmenu"></Script><ScriptName="hint"></Script><ScriptName="jqueryMultiSelect"></Script><ScriptName="timePicker"></Script></Scripts></Page></Pages></Resources>

1.Plugins节点表示每个不同的jQuery插件,Styles和Scripts节点是css文件和js文件集合,即这个jQuery插件的依赖文件.每个文件都有一个key,为了保证文件不重复加载.

2.再看Required节点,Required其实全局js加载,如每张页面都需要菜单,则需要jdMenu插件.这个可以根据需求来调整.

3.Pages节点.

这个节点集合是配置每个具体页面需要用到的插件.

以上三点为基本点,当然每个系统还有其他要注意的,比如有些页面的功能需要有权限的人才能使用,那如果没有权限的人员进入,则与此功能相关的插件则无需加载.具体可以根据需求不同进行扩展.

二.解析资源文件

以上文件可以利用asp.net 2.0的文件依赖缓存在服务器端缓存起来,然后进行解析.我这里代码可能写的比较乱,并不完善.给大家一个参考吧.

CodeusingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Web.UI;
usingSystem.Xml;
usingSystem.Web;
usingSystem.Web.Caching;

namespaceLuna.Common
{
publicclassSiteConfig
{
publicstaticXmlDocument GetResourceXml()
{
            XmlDocument doc
=newXmlDocument();
            ContentCache contentCache
=ContentCache.Instantiate();



if(contentCache["ResourceMulit"]!=null)
{
                doc
=(XmlDocument)contentCache["ResourceMulit"];
            }
else{
stringfile=HttpContext.Current.Server.MapPath("/App_Data/ResourceMulit.xml");
                doc.Load(file);
                CacheDependency dep
=newCacheDependency(file, DateTime.Now);
                contentCache.Insert(
"ResourceMulit", doc, dep);
            }
returndoc;
        }

publicstaticvoidRegsiterResource()
{
            XmlDocument doc
=SiteConfig.GetResourceXml();

            XmlNode firstNode
=doc.DocumentElement.FirstChild;
            XmlNodeList RequiredScripts
=firstNode.FirstChild.ChildNodes;
            XmlNodeList RequiredStyles
=firstNode.ChildNodes[1].ChildNodes;

            Page currentPage
=(Page)HttpContext.Current.Handler;
            RegisterResource(RequiredScripts, RequiredStyles);

            XmlNode secondNode
=doc.DocumentElement.ChildNodes[1];
foreach(XmlNode iteminsecondNode.ChildNodes)
{
                
stringpageSrc=item.Attributes["Src"].Value.ToLower();
if(currentPage.ResolveUrl(pageSrc)==HttpContext.Current.Request.Path.ToLower())
{
                    RegisterResource(item.FirstChild.ChildNodes, item.ChildNodes[
1].ChildNodes);
                }
            }
        }


publicstaticvoidRegsiterResourceKey(XmlNode node, List<string>resourceNode, List<string>removeNode)
{
if(node!=null)
{
                XmlNode scriptsNodes
=node.SelectSingleNode("descendant::Scripts");

foreach(XmlNode scriptNodeinscriptsNodes.ChildNodes)
{
if(scriptNode.Attributes["Removed"]==null)
{
                        resourceNode.Add(scriptNode.Attributes[
"Name"].Value);
                    }
else{
                        removeNode.Add(scriptNode.Attributes[
"Name"].Value);
                    }

                }
            }
        }

publicstaticvoidRegsiterCommon()
{
            XmlDocument doc
=SiteConfig.GetResourceXml();


//XmlNodeList RequiredStyles = firstNode.ChildNodes[1].ChildNodes;List<Plugin>PluginList=RegsiterCommonResource(doc);

//RegisterResource(RequiredScripts, RequiredStyles);
//Pages
            XmlNode currentPageNode
=GetCurrentPageNode(doc);
            XmlNode RequireNode
=GetRequireNode(doc);
            Dictionary
<string,string>scriptList=newDictionary<string,string>();
            Dictionary
<string,string>styleList=newDictionary<string,string>();

            List
<string>removeNode=newList<string>();
            List
<string>resourceNode=newList<string>();
if(currentPageNode!=null)
{
                XmlAttribute isAuthenticated
=currentPageNode.Attributes["IsAuthenticated"];
if(isAuthenticated!=null)
{

if(Convert.ToBoolean(isAuthenticated.Value)&&HttpContext.Current.Request.IsAuthenticated)
{
                        RegsiterResourceKey(currentPageNode, resourceNode, removeNode);
                    }
                }
else{
                    RegsiterResourceKey(currentPageNode, resourceNode, removeNode);
                }
            }
           

            RegsiterResourceKey(RequireNode, resourceNode, removeNode);

           List
<string>filterResourceNode=resourceNode.FindAll(delegate(stringnode)
{
foreach(var iteminremoveNode)
{
returnnode!=item;
                }
returntrue;
            }
);
foreach(var iteminfilterResourceNode)
{
                Plugin plugin
=GetCurrentPlugin(item, PluginList);

                RegisterPlugin(scriptList, styleList, plugin);
            }
        }

publicstaticvoidRegisterPlugin(Dictionary<string,string>scriptList, Dictionary<string,string>styleList, Plugin plugin)
{
            Page currentPage
=(Page)HttpContext.Current.Handler;
foreach(Style styleinplugin.Styles)
{
if(!styleList.ContainsKey(style.Key))
{
                    styleList.Add(style.Key, style.Href);
                    currentPage.AddCss(style.Href);
                }
            }
foreach(Script scriptinplugin.Scripts)
{
if(!scriptList.ContainsKey(script.Key))
{
                    scriptList.Add(script.Key, script.Src);
                    currentPage.AddScript(script.Src);
                }
            }
if(!scriptList.ContainsKey(plugin.Name))
{
                scriptList.Add(plugin.Name, plugin.Src);
                currentPage.AddScript(plugin.Src);
            }
        }

publicstaticPlugin GetCurrentPlugin(stringscriptNode, List<Plugin>PluginList)
{

foreach(Plugin plugininPluginList)
{
if(scriptNode==plugin.Name)
{
returnplugin;
                }
            }
returnPluginList[0];
        }

privatestaticXmlNode GetRequireNode(XmlDocument doc)
{
            XmlNode node
=doc.DocumentElement.SelectSingleNode("descendant::Required");
returnnode;
        }

publicstaticXmlNode GetCurrentPageNode(XmlDocument doc)
{

            Page currentPage
=(Page)HttpContext.Current.Handler;
            XmlNode node
=doc.DocumentElement.SelectSingleNode("descendant::Pages");
foreach(XmlNode iteminnode.ChildNodes)
{

stringpageSrc=item.Attributes["Src"].Value.ToLower();
if(currentPage.ResolveUrl(pageSrc)==HttpContext.Current.Request.Path.ToLower())
{
returnitem;
                }
            }
returnnull;
        }

publicstaticList<Plugin>RegsiterCommonResource(XmlDocument doc)
{
            XmlNode firstNode
=doc.DocumentElement.SelectSingleNode("descendant::Common");
            XmlNodeList PluginNodeList
=firstNode.SelectSingleNode("descendant::Plugins").ChildNodes;

            Page currentPage
=(Page)HttpContext.Current.Handler;
            List
<Plugin>plugins=newList<Plugin>();
foreach(XmlNode iteminPluginNodeList)
{
                Plugin plugin
=newPlugin();
                plugin.Name
=item.Attributes["Name"].Value;
                plugin.Src
=currentPage.ResolveUrl(item.Attributes["Src"].Value);
                
                XmlNodeList scripts
=item.SelectSingleNode("descendant::Scripts").ChildNodes;
foreach(XmlNode scriptinscripts)
{
stringkey=script.Attributes["Key"].Value;
stringsrc=currentPage.ResolveUrl(script.Attributes["Src"].Value);
boolremoved=false;
if(script.Attributes["Removed"]!=null)
{
                        removed
=Convert.ToBoolean(script.Attributes["Removed"].Value);
                    }
                    plugin.Scripts.Add(newScript(key, src,false));
                }
                XmlNode styles=item.SelectSingleNode("descendant::Styles");
                
if(styles!=null)
{
foreach(XmlNode styleinstyles.ChildNodes)
{
                        plugin.Styles.Add(
newStyle(style.Attributes["Key"].Value,currentPage.ResolveUrl(style.Attributes["Href"].Value)));
                    }
                }
                plugins.Add(plugin);
            }
returnplugins;
        }

privatestaticvoidRegisterScript(stringaa)
{
//foreach (XmlNode item in scriptsNode)
//{
//string key = item.Attributes["Name"].Value;

//RegisterScript(key);
//}}

privatestaticvoidRegisterResource(XmlNodeList scripts, XmlNodeList styles)
{
            Page currentPage
=(Page)HttpContext.Current.Handler;
foreach(XmlNode iteminstyles)
{
stringhref=currentPage.ResolveUrl(item.Attributes["Href"].Value);
                LiteralControl control
=newLiteralControl(string.Format("\n<link href=\"{0}\"rel=\"stylesheet\"type=\"text/css\"/>", href));
                currentPage.Header.Controls.Add(control);
            }
foreach(XmlNode iteminscripts)
{
stringkey=item.Attributes["Key"].Value;
stringsrc=currentPage.ResolveUrl(item.Attributes["Src"].Value);
                currentPage.AddScript(src);
//currentPage.ClientScript.RegisterClientScriptInclude(currentPage.GetType(), key, src);}

        }
    }
}

三.定义一个基类Page,与实际页面进行匹配

CodeprotectedoverridevoidOnLoad(EventArgs e)
        {
            SiteConfig.RegsiterCommon();
        }

好了,基本思路就是如此.大家有更好方案可以拿出来讨论下哈.

转载于:https://www.cnblogs.com/Clingingboy/archive/2008/10/08/jQueryPluginsManage.html

jQuery插件管理方案相关推荐

  1. 爱奇艺开源轻量级插件化方案 Neptune

    爱奇艺近日开源了其轻量级插件化方案 Neptune,项目地址:https://github.com/iqiyi/Neptune 插件化框架可以在主程序不重新安装的情况下,针对单个业务模块进行动态加载达 ...

  2. 从零开始学习jQuery (三) 管理jQuery包装集

    本系列文章导航 从零开始学习jQuery (一) 开天辟地入门篇 从零开始学习jQuery (二) 万能的选择器 从零开始学习jQuery (三) 管理jQuery包装集 从零开始学习jQuery ( ...

  3. 40 个让你的网站更加友好的 jQuery 插件

    一个插件的基本功能是执行一个含有元素集合的函数数组.每个方法和jQuery核心组成一个插件,如.fadeOut()或.addClass().一个jQuery插件是一个基本的可以扩充jQuery 原型对 ...

  4. jQuery插件之form

    抓住6月份的尾巴,今天的主题是 今天我想介绍的是一款jQuery的插件:Jquery.form.js官网. 通过该插件,我们可以非常简单的实现表单的异步提交,并实现文件上传.进度条显示等等. 现在我们 ...

  5. 基于Jquery插件Uploadify实现实时显示进度条上传图片

    网址:http://www.jb51.net/article/83811.htm 这篇文章主要介绍了基于Jquery插件Uploadify实现实时显示进度条上传图片的相关资料,感兴趣的小伙伴们可以参考 ...

  6. jQuery:收集一些基于jQuery框架开发的控件/jquery插件2

    Horizontal accordion: jQuery 基于jQuery开发,非常简单的水平方向折叠控件. 主页:http://letmehaveblog.blogspot.com/2007/10/ ...

  7. 15款帮助你实现响应式导航的 jQuery 插件

    对于我们大多数人来说,建立一个负责任的布局中最困难的方面是规划和导航的实现.由于没有真正经得起考验的通用解决方案,您可以使用的菜单设计风格将取决于正在建设的网站类型. 无论你正在建设什么类型的网站,在 ...

  8. jQuery插件之Cookie

    一.jQuery.Cookie.js插件是一个轻量级的Cookie管理插件.下载地址:http://plugins.jquery.com/cookie/ 特别提醒,今日发现一个特别的错误,google ...

  9. jquery 插件和后台模板搜集

    弹框 alert confirm http://www.jq22.com/jquery-info2607 jQuery表格排序筛选插件 http://www.jq22.com/jquery-info8 ...

  10. 50个jQuery插件可将你的网站带到另一个高度

    Web领域一直在发生变化并且其边界在过去的每一天都在发生变化(甚至不能以小时为计),随着其边界的扩展取得了许多新发展.在这些进步之中,开发者的不断工作创造了更大和更好的脚本,这些脚本以插件方式带来更好 ...

最新文章

  1. go系列(5)- beego自己写controller
  2. python print函数用法_Python3.2中Print函数用法实例详解
  3. 如何学习linux编程(转载)
  4. Java的包裹wrap
  5. Apache Spark探秘:三种分布式部署方式比较
  6. android studio 反编译修改versioncode,在android studio 中修改versioncode 跟versionname(示例代码)...
  7. Python--day48--ORM框架SQLAlchemy操作表
  8. Python获取当前目录
  9. 初者Python笔记(案例:用字典无限添加节点)
  10. .net 发送电子邮件
  11. cgicc输出指定编码为utf-8
  12. python基础语法测评_3. Python基础语法
  13. 解决Windows x64bit环境下无法使用PLSQL Developer连接到Oracle DB中的问题
  14. tbschedule 前后置处理器、定期执行任务
  15. 安卓 运行 linux 桌面,安卓上运行原生Xorg和Linux桌面记录[完结]
  16. css 写一个左中右布局占满屏幕,左右两块固定宽度200,中间自适应宽,先加载中间块
  17. High Scalability创始人Todd Hoff:Facebook网络性能的秘密武器
  18. Java模拟登录强智教务系统分析思路
  19. 恩兔NS-1刷ARMBIAN教程
  20. vue子组件修改父组件的值

热门文章

  1. 微信小程序多位验证码/密码输入框
  2. 升级nodejs的方法(3)
  3. 误删除分区下的数据恢复
  4. 关于Ubuntu中passwd、shadow、group等文件
  5. Android Cloud to Device Messaging 服务介绍 – 如何使用云计算推送消息到手机(转载)...
  6. [独库骑行之我们路过高山]玉希莫勒盖达坂顶上的震撼
  7. ORM 革命 —— 复兴 | ORM Revolution -- Revived
  8. 《SQL Server 2005开发技术大全》分享一本书
  9. 多进程相关内容(IPC)
  10. English trip M1 - AC9 Nosey people 爱管闲事的人 Teacher:Solo