jquery创造二个新的节点目的(自定义结构/内容)的好点子

自家曾在一家玩耍集团,最近在做四个类别,须要做二个威他霉素的管住后台,须求调用其余公司提供的接口,来贯彻后台管理的操作

HTML

是因为接口地址都是固定的,所以想到利用自定义节点,来将接口都配置到web.config中。

复制代码 代码如下:

异常快,v1.0本子出炉: 

<!DOCTYPE html>
<html>
<head>
<script class=”jsbin”
src=”http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt;
<meta charset=utf-8 />
<title>JsBin-在线js/css调节和测量试验工具</title>
<!–[if IE]>
<script
src=”http://html5shiv.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt;
<![endif]–>
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
</style>
</head>
<body>
<p id=”hello”>Hello OSTools</p>
</body>
</html>

public class RequestConfigSection : ConfigurationSection    {        [ConfigurationProperty("sources", IsDefaultCollection = true)]        [ConfigurationCollection(typeof(RequestConfigSourceCollection), AddItemName = "add")]        public RequestConfigSourceCollection ConfigCollection        {            get { return (RequestConfigSourceCollection)this["sources"]; }            set { this["sources"] = value; }        }    }        public class RequestConfigSourceCollection : ConfigurationElementCollection    {        /// <summary>        /// 创建新元素        /// </summary>        /// <returns></returns>        protected override ConfigurationElement CreateNewElement()        {            return new RequestConfigSource();        }        /// <summary>        /// 获取元素的键        /// </summary>        /// <param name="element"></param>        /// <returns></returns>        protected override object GetElementKey(ConfigurationElement element)        {            return ((RequestConfigSource)element).Name;        }        /// <summary>        /// 获取所有键        /// </summary>        public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } }        /// <summary>        /// 索引器        /// </summary>        /// <param name="name"></param>        /// <returns></returns>        public new RequestConfigSource this[string name]        {            get { return (RequestConfigSource)BaseGet; }        }    }    public class RequestConfigSource : ConfigurationElement    {        /// <summary>        /// 名称        /// </summary>        [ConfigurationProperty]        public string Name        {            get { return this["name"]; }            set { this["name"] = value; }        }        /// <summary>        /// 地址        /// </summary>        [ConfigurationProperty]        public string Url        {            get { return this["url"]; }            set { this["url"] = value; }        }        /// <summary>        /// 访问类型        /// </summary>        [ConfigurationProperty]        public RequestType RequestType        {            get            {                return (RequestType)Enum.Parse(typeof(RequestType), this["type"].ToString;            }            set { this["type"] = value; }        }    }

JavaScript

在web.config中的配置形式为:

复制代码 代码如下:

<apiRequestConfig>  <sources>    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />  </sources></apiRequestConfig>

//jq创制多少个新的节点目的;
//例如:<div id=’mydiv’ class=’menu’></div>
var objNewDiv = $(‘<div>’,{‘id’:’mydiv’,’class’:’menu’});
objNewDiv.html(123456);
$(‘#hello’).append(objNewDiv);
console.log(objNewDiv);

这时又看了三回必要文档,发掘有表达分裂平台的接口地址是不平等的,但接口做的事情是同样的。

结果
图片 1

下一场就先导想,若是随着在底下追加,则不一致平台的平等接口的名号是不可能长久以来的。

您只怕感兴趣的稿子:

由此想到的优质的布置情势为:

<apiRequestConfig>  <sources platform="android">    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />  </sources>  <sources platform="ios">    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />    <add name="..." url="..." type="POST" />  </sources></apiRequestConfig>

可是sources 名称的节点只好出现贰遍…好吧,蛋疼了。

商量尝试了一午夜也从没找到确切的消除格局,又懒得再另行写一套代码来读取XML,…初始在网络搜设计方案

用汉语做首要字找不着…翻了墙,用立陶宛(Lithuania)语来当第一字 one or more
ConfigurationElementCollection…

最后在一老外的博客里找到了二个代替的技术方案,最后的陈设为:

<apiRequestConfig>    <requestConfigs>      <request platform="android">        <sources>          <add name="..." url="..." type="POST" />          <add name="..." url="..." type="POST" />          <add name="..." url="..." type="POST" />        </sources>      </request>      <request platform="ios">        <sources>          <add name="..." url="..." type="POST" />          <add name="..." url="..." type="POST" />          <add name="..." url="..." type="POST" />        </sources>      </request>    </requestConfigs>  </apiRequestConfig>

C#代码如下:

public class RequestConfigSection : ConfigurationSection    {        [ConfigurationProperty("requestConfigs", IsDefaultCollection = true)]        [ConfigurationCollection(typeof(RequestConfigTypeCollection), AddItemName = "request")]        public RequestConfigTypeCollection ConfigCollection        {            get { return (RequestConfigTypeCollection)this["requestConfigs"]; }            set { this["requestConfigs"] = value; }        }        /// <summary>        /// 根据平台和名称获取请求配置信息        /// </summary>        /// <param name="name"></param>        /// <param name="platform"></param>        /// <returns></returns>        public RequestConfigSource GetRequestConfigSource(string platform, string name)        {            return ConfigCollection[platform].SourceCollection[name];        }    }    public class RequestConfigTypeCollection : ConfigurationElementCollection    {        /// <summary>        /// 创建新元素        /// </summary>        /// <returns></returns>        protected override ConfigurationElement CreateNewElement()        {            return new RequestConfigType();        }        /// <summary>        /// 获取元素的键        /// </summary>        /// <param name="element"></param>        /// <returns></returns>        protected override object GetElementKey(ConfigurationElement element)        {            return ((RequestConfigType)element).Platform;        }        /// <summary>        /// 获取所有键        /// </summary>        public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } }        /// <summary>        /// 索引器        /// </summary>        /// <param name="name"></param>        /// <returns></returns>        public new RequestConfigType this[string platform]        {            get { return (RequestConfigType)BaseGet; }        }    }    public class RequestConfigType : ConfigurationElement    {        /// <summary>        /// 获取全部请求配置信息        /// </summary>        /// <returns></returns>        public RequestConfigSource[] GetAllRequestSource()        {            var keys = this.SourceCollection.AllKeys;            return keys.Select(name => this.SourceCollection[name]).ToArray();        }        /// <summary>        /// 平台标识        /// </summary>        [ConfigurationProperty("platform")]        public string Platform        {            get { return this["platform"]; }            set { this["platform"] = value; }        }        [ConfigurationProperty("sources", IsDefaultCollection = true)]        [ConfigurationCollection(typeof(RequestConfigSourceCollection), AddItemName = "add")]        public RequestConfigSourceCollection SourceCollection        {            get { return (RequestConfigSourceCollection)this["sources"]; }            set { this["sources"] = value; }        }    }    public class RequestConfigSourceCollection : ConfigurationElementCollection    {        /// <summary>        /// 创建新元素        /// </summary>        /// <returns></returns>        protected override ConfigurationElement CreateNewElement()        {            return new RequestConfigSource();        }        /// <summary>        /// 获取元素的键        /// </summary>        /// <param name="element"></param>        /// <returns></returns>        protected override object GetElementKey(ConfigurationElement element)        {            return ((RequestConfigSource)element).Name;        }        /// <summary>        /// 获取所有键        /// </summary>        public IEnumerable<string> AllKeys { get { return BaseGetAllKeys().Cast<string>(); } }        /// <summary>        /// 索引器        /// </summary>        /// <param name="name"></param>        /// <returns></returns>        public new RequestConfigSource this[string name]        {            get { return (RequestConfigSource)BaseGet; }        }    }    /// <summary>    /// 请求的配置信息    /// </summary>    public class RequestConfigSource : ConfigurationElement    {        /// <summary>        /// 名称        /// </summary>        [ConfigurationProperty]        public string Name        {            get { return this["name"]; }            set { this["name"] = value; }        }        /// <summary>        /// 地址        /// </summary>        [ConfigurationProperty]        public string Url        {            get { return this["url"]; }            set { this["url"] = value; }        }        /// <summary>        /// 访问类型        /// </summary>        [ConfigurationProperty]        public RequestType RequestType        {            get            {                return (RequestType)Enum.Parse(typeof(RequestType), this["type"].ToString;            }            set { this["type"] = value; }        }    }

本身的花费境况为 .net framework 4.0

最初RequestConfigSection 类中的ConfigCollection
RequestConfigType 类中的SourceCollection
未有概念ConfigurationCollection特性

而是在RequestConfigTypeCollectionRequestConfigTypeCollection
中重载了ElementName属性,重临子级的节点名。

结果抛出节点名未定义的不行…

改由特性ConfigurationCollection概念,并给天性属性AddItemName赋值为子级的节点名
化解…

鬼子博客地址:http://tneustaedter.blogspot.com/2011/09/how-to-create-one-or-more-nested.html
需要FQ访问

抑或直接看那儿,我给Copy出来了:http://www.cnblogs.com/efenghuo/articles/4022836.html

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图