jQuery发展趋势一片大好,这里向大家介绍一种管理jQuery插件的方案。可能有些人已经在系统中已经使用.
使用原因:在开发过程中,jQuery插件的使用越来越多,且jQuery的某些插件是基于某些插件来使用了,且有先后顺序的问题.
最初的做法:直接在页面上加载js.如下代码,其中使用了一些插件,其依赖于jQuery.
其中用了一个jdMenu插件,其依赖于四个文件(jdMenu.css,jquery.js,bgiframe.min.js,positionBy.js)

Code
<link href="/App_Scripts/jPlugin/jdMenu/jquery.jdMenu.css" rel="stylesheet" type="text/css" />
<script src="/App_Scripts/jquery.js" type="text/javascript"></script>
<script src="/App_Scripts/jPlugin/jquery-bgiframe/jquery.bgiframe.min.js" type="text/javascript"></script>
<script src="/App_Scripts/jPlugin/jdMenu/jquery.positionBy.js" type="text/javascript"></script>
<script src="/App_Scripts/jPlugin/jdMenu/jquery.jdMenu.js" type="text/javascript"></script>
<script src="/App_Scripts/jPlugin/jquery-hint/hint.jquery.js" type="text/javascript"></script>
<script src="/App_Scripts/jPlugin/utilities/customExt.js" type="text/javascript"></script>
<script src="/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>
<Plugin Name="cookie" Src="~/App_Scripts/jPlugin/cookie/cookie.pack.js">
<Styles>
</Styles>
<Scripts>
<Script Key="jquery" Src="~/App_Scripts/jquery.js" />
</Scripts>
</Plugin>
<Plugin Name="treeview" Src="~/App_Scripts/jPlugin/jquery-treeview/jquery.treeview.pack.js">
<Styles>
<Style Key="jquery_treeview_css" Href="~/App_Scripts/jPlugin/jquery-treeview/jquery.treeview.css" />
</Styles>
<Scripts>
<Script Key="jquery" Src="~/App_Scripts/jquery.js" />
</Scripts>
</Plugin>
<Plugin Name="autocomplete" Src="~/App_Scripts/jPlugin/jquery-autocomplete/jquery.autocomplete-bundle.pack.js">
<Styles>
<Style Key="autocomplete_css" Href="~/App_Scripts/jPlugin/jquery-autocomplete/jquery.autocomplete.css" />
</Styles>
<Scripts>
<Script Key="jquery" Src="~/App_Scripts/jquery.js" />
<Script Key="bgiframe" Src="~/App_Scripts/jPlugin/jquery-bgiframe/jquery.bgiframe.min.js" />
</Scripts>
</Plugin>
</Plugins>
</Common>
<Required>
<Scripts>
<Script Name="jdMenu"></Script>
<Script Name="hint"></Script>
<Script Name="jQueryCustomExt"></Script>
<Script Name="cookie"></Script>
</Scripts>
</Required>
<Pages>
<Page Src="~/Administration/EmployeeMgmt.aspx">
<Scripts>
<Script Name="jTemplates"></Script>
<Script Name="treeview"></Script>
<Script Name="scrollTo"></Script>
<Script Name="validate"></Script>
<Script Name="jQueryCustomExt"></Script>
<Script Name="autocomplete"></Script>
<Script Name="blockUI"></Script>
</Scripts>
</Page>
<Page Src="~/ScheduleMgmt/Reschedule.aspx">
<Scripts>
<Script Name="My97DatePicker"></Script>
<Script Name="jQueryCustomExt"></Script>
<Script Name="draggable_1.0"></Script>
<Script Name="jTemplates"></Script>
<Script Name="tab_1.0"></Script>
<Script Name="autocomplete"></Script>
<Script Name="spinBtn"></Script>
<Script Name="hotkeys"></Script>
<Script Name="datepicker"></Script>
<Script Name="clockpick"></Script>
<Script Name="validate"></Script>
<Script Name="blockUI"></Script>
<Script Name="tooltip"></Script>
<Script Name="contextmenu"></Script>
<Script Name="hint"></Script>
<Script Name="jqueryMultiSelect"></Script>
<Script Name="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的文件依赖缓存在服务器端缓存起来,然后进行解析.我这里代码可能写的比较乱,并不完善.给大家一个参考吧.

Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Xml;
using System.Web;
using System.Web.Caching;

namespace Luna.Common


{
public class SiteConfig

{
public static XmlDocument GetResourceXml()

{
XmlDocument doc = new XmlDocument();
ContentCache contentCache = ContentCache.Instantiate();



if (contentCache["ResourceMulit"] != null)

{
doc = (XmlDocument)contentCache["ResourceMulit"];
}
else

{
string file = HttpContext.Current.Server.MapPath("/App_Data/ResourceMulit.xml");
doc.Load(file);
CacheDependency dep = new CacheDependency(file, DateTime.Now);
contentCache.Insert("ResourceMulit", doc, dep);
}
return doc;
}

public static void RegsiterResource()

{
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 item in secondNode.ChildNodes)

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

{
RegisterResource(item.FirstChild.ChildNodes, item.ChildNodes[1].ChildNodes);
}
}
}


public static void RegsiterResourceKey(XmlNode node, List<string> resourceNode, List<string> removeNode)

{
if (node != null)

{
XmlNode scriptsNodes = node.SelectSingleNode("descendant::Scripts");

foreach (XmlNode scriptNode in scriptsNodes.ChildNodes)

{
if (scriptNode.Attributes["Removed"] == null)

{
resourceNode.Add(scriptNode.Attributes["Name"].Value);
}
else

{
removeNode.Add(scriptNode.Attributes["Name"].Value);
}

}
}
}

public static void RegsiterCommon()

{
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 = new Dictionary<string, string>();
Dictionary<string, string> styleList = new Dictionary<string, string>();

List<string> removeNode = new List<string>();
List<string> resourceNode = new List<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(string node)

{
foreach (var item in removeNode)

{
return node != item;
}
return true;
});
foreach (var item in filterResourceNode)

{
Plugin plugin = GetCurrentPlugin(item, PluginList);

RegisterPlugin(scriptList, styleList, plugin);
}
}

public static void RegisterPlugin(Dictionary<string, string> scriptList, Dictionary<string, string> styleList, Plugin plugin)

{
Page currentPage = (Page)HttpContext.Current.Handler;
foreach (Style style in plugin.Styles)

{
if (!styleList.ContainsKey(style.Key))

{
styleList.Add(style.Key, style.Href);
currentPage.AddCss(style.Href);
}
}
foreach (Script script in plugin.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);
}
}

public static Plugin GetCurrentPlugin(string scriptNode, List<Plugin> PluginList)

{

foreach (Plugin plugin in PluginList)

{
if (scriptNode == plugin.Name)

{
return plugin;
}
}
return PluginList[0];
}

private static XmlNode GetRequireNode(XmlDocument doc)

{
XmlNode node = doc.DocumentElement.SelectSingleNode("descendant::Required");
return node;
}

public static XmlNode GetCurrentPageNode(XmlDocument doc)

{

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

{

string pageSrc = item.Attributes["Src"].Value.ToLower();
if (currentPage.ResolveUrl(pageSrc) == HttpContext.Current.Request.Path.ToLower())

{
return item;
}
}
return null;
}

public static List<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 = new List<Plugin>();
foreach (XmlNode item in PluginNodeList)

{
Plugin plugin = new Plugin();
plugin.Name = item.Attributes["Name"].Value;
plugin.Src = currentPage.ResolveUrl(item.Attributes["Src"].Value);
XmlNodeList scripts = item.SelectSingleNode("descendant::Scripts").ChildNodes;
foreach (XmlNode script in scripts)

{
string key = script.Attributes["Key"].Value;
string src = currentPage.ResolveUrl(script.Attributes["Src"].Value);
bool removed = false;
if (script.Attributes["Removed"] != null)

{
removed = Convert.ToBoolean(script.Attributes["Removed"].Value);
}
plugin.Scripts.Add(new Script(key, src,false));
}
XmlNode styles = item.SelectSingleNode("descendant::Styles");
if (styles != null)

{
foreach (XmlNode style in styles.ChildNodes)

{
plugin.Styles.Add(new Style(style.Attributes["Key"].Value,currentPage.ResolveUrl(style.Attributes["Href"].Value)));
}
}
plugins.Add(plugin);
}
return plugins;
}

private static void RegisterScript(string aa)

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

// RegisterScript(key);
//}
}

private static void RegisterResource(XmlNodeList scripts, XmlNodeList styles)

{
Page currentPage = (Page)HttpContext.Current.Handler;
foreach (XmlNode item in styles)

{
string href = currentPage.ResolveUrl(item.Attributes["Href"].Value);

LiteralControl control = new LiteralControl(string.Format("\n<link href=\"
{0}\" rel=\"stylesheet\" type=\"text/css\" />", href));
currentPage.Header.Controls.Add(control);
}
foreach (XmlNode item in scripts)

{
string key = item.Attributes["Key"].Value;
string src = currentPage.ResolveUrl(item.Attributes["Src"].Value);
currentPage.AddScript(src);
//currentPage.ClientScript.RegisterClientScriptInclude(currentPage.GetType(), key, src);
}

}
}
}

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

Code
protected override void OnLoad(EventArgs e)
{
SiteConfig.RegsiterCommon();
}
好了,基本思路就是如此.大家有更好方案可以拿出来讨论下哈.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!