<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>蓝色的华 &#187; web dev</title>
	<atom:link href="http://bluehua.org/category/web-dev/feed" rel="self" type="application/rss+xml" />
	<link>http://bluehua.org</link>
	<description>分享所学,backup一切~</description>
	<lastBuildDate>Fri, 13 Apr 2012 14:08:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>node.js中mysql-native的掉线重联</title>
		<link>http://bluehua.org/2012/03/21/1826.html</link>
		<comments>http://bluehua.org/2012/03/21/1826.html#comments</comments>
		<pubDate>Wed, 21 Mar 2012 14:35:25 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mysql-native]]></category>
		<category><![CDATA[node.js]]></category>

		<guid isPermaLink="false">http://bluehua.org/?p=1826</guid>
		<description><![CDATA[由于受一篇性能测试的文章的影响，选择了mysql-native作为node的mysql客户端，由于没有自动重联的功能,长时间运行后就会掉线,所以加了一段判断掉线的代码: var Mysql = null; &#160; function MysqlConnect&#40;&#41; &#123; //... } &#160; MysqlSafeQuery = function&#40;sql&#41; &#123; if &#40;!Mysql &#124;&#124; !Mysql.connection.writable&#41; &#123; MysqlConnect&#40;&#41;; &#125; &#160; return Mysql.query&#40;sql&#41;; &#125;]]></description>
			<content:encoded><![CDATA[<p>由于受一篇<a href="http://cnodejs.org/topic/4f16442ccae1f4aa2700112f">性能测试的文章</a>的影响，选择了mysql-native作为node的mysql客户端，由于没有自动重联的功能,长时间运行后就会掉线,所以加了一段判断掉线的代码:</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Mysql <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> MysqlConnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">//... }</span>
&nbsp;
MysqlSafeQuery <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Mysql <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>Mysql.<span style="color: #660066;">connection</span>.<span style="color: #660066;">writable</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        MysqlConnect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">return</span> Mysql.<span style="color: #660066;">query</span><span style="color: #009900;">&#40;</span>sql<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2012/03/21/1826.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JsonStream.js</title>
		<link>http://bluehua.org/2012/03/21/1808.html</link>
		<comments>http://bluehua.org/2012/03/21/1808.html#comments</comments>
		<pubDate>Wed, 21 Mar 2012 13:02:32 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JsonStream]]></category>
		<category><![CDATA[node.js]]></category>

		<guid isPermaLink="false">http://bluehua.org/?p=1808</guid>
		<description><![CDATA[JsonStream.js是一个用于node.js的双向通信协议实现，它使用json作为数据包格式，可以用于socket，也可以用于http长连接,同时自带了简单的rpc实现。这段代码现在被用于一个对实时性要求比较高的小项目，本来打算用python+twisted做，但是后来临时决定尝试一下node.js，下面举例说明用法: 第一个例子，最简单的应用，基于socket通信,发送和接受json数据 StreamServer.js var Net = require&#40;'net'&#41;; var JsonStream = require&#40;'./JsonStream.js'&#41;; &#160; //创建一个普通的socket server var server = Net.createServer&#40;function&#40;c&#41; &#123; //当有客户端连接后,socket连接之上创建一个JsonStream var stream = new JsonStream.Stream&#40;c&#41;; &#160; //监听客户端发来的json数据 stream.on&#40;'json', function&#40;json&#41; &#123; console.log&#40;'receiv json from client:'&#41;; console.log&#40;json&#41;; &#125;&#41;; &#160; //没三秒钟向客户端发送一个json数据 setInterval&#40;function&#40;&#41;&#123; stream.write&#40;&#123; data:'hello client', time:new Date&#40;&#41; &#125;&#41;; &#125;, 3000&#41;; &#125;&#41;; &#160; server.listen&#40;7777&#41;; 第二个例子,基于socket，两端互相rpc调用 RpcServer.js var Net = [...]]]></description>
			<content:encoded><![CDATA[<p>JsonStream.js是一个用于node.js的双向通信协议实现，它使用json作为数据包格式，可以用于socket，也可以用于http长连接,同时自带了简单的rpc实现。这段代码现在被用于一个对实时性要求比较高的小项目，本来打算用python+twisted做，但是后来临时决定尝试一下node.js，下面举例说明用法:</p>
<p>第一个例子，最简单的应用，基于socket通信,发送和接受json数据<br />
StreamServer.js</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Net <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'net'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> JsonStream <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./JsonStream.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//创建一个普通的socket server</span>
<span style="color: #003366; font-weight: bold;">var</span> server <span style="color: #339933;">=</span> Net.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//当有客户端连接后,socket连接之上创建一个JsonStream</span>
    <span style="color: #003366; font-weight: bold;">var</span> stream <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> JsonStream.<span style="color: #660066;">Stream</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//监听客户端发来的json数据</span>
    stream.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'json'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'receiv json from client:'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//没三秒钟向客户端发送一个json数据</span>
    setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        stream.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            data<span style="color: #339933;">:</span><span style="color: #3366CC;">'hello client'</span><span style="color: #339933;">,</span>
            time<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
server.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7777</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>第二个例子,基于socket，两端互相rpc调用<br />
RpcServer.js</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Net <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'net'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> JsonStream <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./JsonStream.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//定义服务端支持的Rpc接口</span>
<span style="color: #003366; font-weight: bold;">var</span> ServerApi <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//这是一个普通的接口,直接返回数据</span>
    hello<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'client call hello'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">'hello '</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//这是一个异步返回的方法</span>
    <span style="color: #006600; font-style: italic;">//这里的SimpleDeferred的确不是一个全功能的Deffered</span>
    <span style="color: #006600; font-style: italic;">//只能用在异步返回的情况</span>
&nbsp;
    helloAfter<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #339933;">,</span> delay<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'client call helloAfter'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> defer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> JsonStream.<span style="color: #660066;">SimpleDeferred</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            defer.<span style="color: #660066;">done</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hello '</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' after '</span> <span style="color: #339933;">+</span> delay <span style="color: #339933;">+</span> <span style="color: #3366CC;">'s'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> delay <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> defer<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> server <span style="color: #339933;">=</span> Net.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> stream <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> JsonStream.<span style="color: #660066;">Stream</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    stream.<span style="color: #660066;">delegate</span> <span style="color: #339933;">=</span> ServerApi<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//每3秒调用一次客户端的say方法</span>
    setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        stream.<span style="color: #660066;">rpcCall</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'say'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'你好!'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
server.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7777</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>RpcClient.js中的调用部分</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;">stream.<span style="color: #660066;">rpcCall</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hello'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'client a'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
stream.<span style="color: #660066;">rpcCall</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'helloAfter'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'client a'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>res<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>第三个例子，用于长连接的http双向通信,由于客户端的代码和现有的业务框架结合比较紧密，暂不提供，只示意一下Server端的代码,基本使用方法和socket的类似</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'http'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> JsonStream <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'./JsonStream.js'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//Rpc接口定义</span>
<span style="color: #003366; font-weight: bold;">var</span> ServerApi <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">//创建一个http server</span>
<span style="color: #003366; font-weight: bold;">var</span> HttpServer <span style="color: #339933;">=</span> Http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//让JsonStream处理所有http请求</span>
    JsonStream.<span style="color: #660066;">HttpStream</span>.<span style="color: #660066;">acceptRequest</span><span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
JsonStream.<span style="color: #660066;">HttpStream</span>.<span style="color: #660066;">events</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>stream<span style="color: #339933;">,</span> request<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'new http stream !!!!!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    stream.<span style="color: #660066;">delegate</span> <span style="color: #339933;">=</span> ServerApi<span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">//发送json到浏览器端</span>
    stream.<span style="color: #000066; font-weight: bold;">write</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        data<span style="color: #339933;">:</span><span style="color: #3366CC;">'hello browser'</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
HttpServer.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">80</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//长连接的最大时长设为30秒</span>
JsonStream.<span style="color: #660066;">HttpStream</span>.<span style="color: #660066;">checkTimeOut</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>更多细节详见源码:),经过一段时间的运行，代码已趋于稳定，可以通过svn获取代码和示例,请容忍我的不爱写注释，呵呵～:</p>
<pre>

http://code-of-emptyhua.googlecode.com/svn/trunk/nodejs/jsonstream
</pre>
]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2012/03/21/1808.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>comet的两个小问题</title>
		<link>http://bluehua.org/2011/05/31/1628.html</link>
		<comments>http://bluehua.org/2011/05/31/1628.html#comments</comments>
		<pubDate>Tue, 31 May 2011 01:06:04 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[comet]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[longpoll]]></category>

		<guid isPermaLink="false">http://bluehua.org/?p=1628</guid>
		<description><![CDATA[最近做了一个webim的项目，使用长连接做push，在chrome和ie下有些小问题 1. chrome的左下角下一直显示等待push.xx.com的响应 解决方法：接到请求立刻返回http头就可以了，不等到有了推送再返回http头 2.ie下对于每个域名同一时刻最多只能有两个并发请求，如果开多个窗口（这个限制不是针对单个窗口的），有的请求便会被阻塞。 解决方法：对每个窗口使用不同的请求域名 使用三级泛域名配置起来最简单，例如：123.push.xx.com.但是JS里涉及到跨域的问题，所以还是要使用push-123.xx.com这样的二级域名。查了下bind9的手册可以这么搞 ;示例 $GENERATE 0-100000 push-$ IN A xx.xx.xx.xx 这样ie下JS可以用时间戳或随机数决定一个domain去请求，不会产生阻塞]]></description>
			<content:encoded><![CDATA[<p>最近做了一个webim的项目，使用长连接做push，在chrome和ie下有些小问题</p>
<p>1. chrome的左下角下一直显示等待push.xx.com的响应</p>
<p>解决方法：接到请求立刻返回http头就可以了，不等到有了推送再返回http头</p>
<p>2.ie下对于每个域名同一时刻最多只能有两个并发请求，如果开多个窗口（这个限制不是针对单个窗口的），有的请求便会被阻塞。</p>
<p>解决方法：对每个窗口使用不同的请求域名<br />
使用三级泛域名配置起来最简单，例如：123.push.xx.com.但是JS里涉及到跨域的问题，所以还是要使用push-123.xx.com这样的二级域名。查了下bind9的手册可以这么搞</p>

<div class="wp_syntax"><div class="code overflow"><pre class="text" style="font-family:monospace;">;示例
$GENERATE 0-100000 push-$ IN A xx.xx.xx.xx</pre></div></div>

<p>这样ie下JS可以用时间戳或随机数决定一个domain去请求，不会产生阻塞</p>
]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2011/05/31/1628.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>接上一篇</title>
		<link>http://bluehua.org/2010/11/24/1466.html</link>
		<comments>http://bluehua.org/2010/11/24/1466.html#comments</comments>
		<pubDate>Wed, 24 Nov 2010 05:24:19 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[Makefile]]></category>

		<guid isPermaLink="false">http://bluehua.org/2010/11/24/1466.html</guid>
		<description><![CDATA[有必要给blogo2加个追加功能。。可以直接把mail追加到最新一篇blog的后面 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; gcc之预处理js和as,下面的makefile主要处理flash的两个版本，SSL版本要比普通版本大150K。。感谢gcc FLAGS=&#34;-DDEBUG&#34; &#160; define make-all gcc $(FLAGS) $1 -E -x c src/swfsocket.js -o $2/swfsocket.js gcc $(FLAGS) $1 -E -x c src/swf/SwfSocket.rel.as -o src/swf/SwfSocket.as sed -i &#34;/^#/d&#34; $2/swfsocket.js sed -i &#34;/^#/d&#34; src/swf/SwfSocket.as cd src/swf; \ mxmlc -static-link-runtime-shared-libraries -output=./SwfSocket.swf SwfSocketMain.as mv src/swf/SwfSocket.swf $2 endef &#160; main: $(call make-all,,bin) $(call make-all,-DSSL,bin_ssl) &#8212;&#8212;&#8212;&#8211; post by gmail~]]></description>
			<content:encoded><![CDATA[<p>有必要给blogo2加个追加功能。。可以直接把mail追加到最新一篇blog的后面<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
gcc之预处理js和as,下面的makefile主要处理flash的两个版本，SSL版本要比普通版本大150K。。感谢gcc</p>

<div class="wp_syntax"><div class="code overflow"><pre class="makefile" style="font-family:monospace;">FLAGS=&quot;-DDEBUG&quot;
&nbsp;
define make-all
	gcc $(FLAGS) $1 -E -x c src/swfsocket.js -o $2/swfsocket.js 
	gcc $(FLAGS) $1 -E -x c src/swf/SwfSocket.rel.as -o src/swf/SwfSocket.as
	sed -i &quot;/^#/d&quot; $2/swfsocket.js
	sed -i &quot;/^#/d&quot; src/swf/SwfSocket.as
	cd src/swf; \
	mxmlc -static-link-runtime-shared-libraries -output=./SwfSocket.swf SwfSocketMain.as
	mv src/swf/SwfSocket.swf $2 
endef
&nbsp;
main:
	$(call make-all,,bin)
	$(call make-all,-DSSL,bin_ssl)</pre></div></div>

<p>&#8212;&#8212;&#8212;&#8211;<br />
post by gmail~</p>
]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2010/11/24/1466.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>gcc预处理用于其他语言也很好用啊</title>
		<link>http://bluehua.org/2010/11/24/1465.html</link>
		<comments>http://bluehua.org/2010/11/24/1465.html#comments</comments>
		<pubDate>Wed, 24 Nov 2010 01:54:26 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[gcc]]></category>

		<guid isPermaLink="false">http://bluehua.org/2010/11/24/1465.html</guid>
		<description><![CDATA[header.js function SSL_socket&#40;&#41; &#123; alert&#40;'SSL'&#41;; &#125; &#160; function socket&#40;&#41; &#123; alert&#40;'normal'&#41;; &#125; main.js #include &#34;header.js&#34; #ifdef _SSL var socket = new SSL_socket&#40;&#41;; #else var socket = new socket&#40;&#41;; #endif gcc -E -x c main.js # 1 "main.js" # 1 "" # 1 "" # 1 "main.js" # 1 "header.js" 1 function SSL_socket() { alert('SSL'); } [...]]]></description>
			<content:encoded><![CDATA[<p>header.js</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> SSL_socket<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'SSL'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> socket<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'normal'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>main.js</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;">#include <span style="color: #3366CC;">&quot;header.js&quot;</span>
#ifdef _SSL
<span style="color: #003366; font-weight: bold;">var</span> socket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> SSL_socket<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
#else
<span style="color: #003366; font-weight: bold;">var</span> socket <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> socket<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
#endif</pre></div></div>

<pre>
gcc -E -x c main.js
# 1 "main.js"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.js"
# 1 "header.js" 1
function SSL_socket()
{
    alert('SSL');
}

function socket()
{
    alert('normal');
}
# 2 "main.js" 2

var socket = new socket();
</pre>
<pre>
gcc -E -x c -D_SSL main.js
# 1 "main.js"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.js"
# 1 "header.js" 1
function SSL_socket()
{
    alert('SSL');
}

function socket()
{
    alert('normal');
}
# 2 "main.js" 2

var socket = new SSL_socket();
</pre>
<p>&#8212;&#8212;&#8212;&#8211;<br />
post by gmail~</p>
]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2010/11/24/1465.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>相对完美的设计思路~</title>
		<link>http://bluehua.org/2010/07/22/1394.html</link>
		<comments>http://bluehua.org/2010/07/22/1394.html#comments</comments>
		<pubDate>Wed, 21 Jul 2010 16:27:31 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[all in one]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[web.py]]></category>

		<guid isPermaLink="false">http://bluehua.org/2010/07/22/1394.html</guid>
		<description><![CDATA[有没有可能写一套代码适应以下三种情况: 1. 客户端没有开启javascript，或者使用ucweb一类的弱智浏览器访问时，功能没有影响 2. 客户端开启了javascript，每个页面都有单独加载的js,但是禁用ajax,只帮助用户交互 3. 客户端开启了javascript，为了提高载入速度，尽量使所有操作用ajax完成 略有所得: http://www.blogo2.com 源代码: http://code-of-emptyhua.googlecode.com/svn/trunk/blogo2 没有禁用js的时候，页面切换，表单提交（注册和登录没有用）尽量的使用ajax，而且部分操作会使用弹层(登录后的管理),禁用js之后，则所有操作换成普通表单提交在页面之间跳来跳去。。 还有一个功能没有开启，就是可以选择不用ajax，但是独立开的页面js依然有效。 有兴趣的同学可以check源代码，这种模式的实现: 1. 开发的时候禁用ajax，单个页面开发，而且要求禁用js时业务可以走通，页面内的js和css统一由模板引入。 2. 对需要开启ajax的链接增加hook，例如data-ajax=”dialog:{}”,则表明在开启ajax模式后，点击这个链接需要弹窗 3. ajax模式下只需要对单个页面更换特有的模板，动态加载需要的js和css即可。 这种设计的特点: 1. 尽量一个人包前后端。。 2. json不潮了，ajax返回html代码，优点是js不用套页面了，缺点是流量大 over，虽然没有完美的代码，但是我尽量~ &#8212;&#8212;&#8212;&#8211; post by gmail~]]></description>
			<content:encoded><![CDATA[<p>有没有可能写一套代码适应以下三种情况:<br />
1. 客户端没有开启javascript，或者使用ucweb一类的弱智浏览器访问时，功能没有影响<br />
2. 客户端开启了javascript，每个页面都有单独加载的js,但是禁用ajax,只帮助用户交互<br />
3. 客户端开启了javascript，为了提高载入速度，尽量使所有操作用ajax完成</p>
<p>略有所得: <a href="http://www.blogo2.com">http://www.blogo2.com</a><br />
源代码: <a href="http://code-of-emptyhua.googlecode.com/svn/trunk/blogo2">http://code-of-emptyhua.googlecode.com/svn/trunk/blogo2</a></p>
<p>没有禁用js的时候，页面切换，表单提交（注册和登录没有用）尽量的使用ajax，而且部分操作会使用弹层(登录后的管理),禁用js之后，则所有操作换成普通表单提交在页面之间跳来跳去。。</p>
<p>还有一个功能没有开启，就是可以选择不用ajax，但是独立开的页面js依然有效。</p>
<p>有兴趣的同学可以check源代码，这种模式的实现:<br />
1. 开发的时候禁用ajax，单个页面开发，而且要求禁用js时业务可以走通，页面内的js和css统一由模板引入。<br />
2. 对需要开启ajax的链接增加hook，例如data-ajax=”dialog:{}”,则表明在开启ajax模式后，点击这个链接需要弹窗<br />
3. ajax模式下只需要对单个页面更换特有的模板，动态加载需要的js和css即可。</p>
<p>这种设计的特点:<br />
1. 尽量一个人包前后端。。<br />
2. json不潮了，ajax返回html代码，优点是js不用套页面了，缺点是流量大</p>
<p>over，虽然没有完美的代码，但是我尽量~</p>
<p>&#8212;&#8212;&#8212;&#8211;<br />
post by gmail~</p>
]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2010/07/22/1394.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ie-css3.htc的修正增强版</title>
		<link>http://bluehua.org/2010/07/04/1327.html</link>
		<comments>http://bluehua.org/2010/07/04/1327.html#comments</comments>
		<pubDate>Sun, 04 Jul 2010 01:36:42 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[behavior]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[ie]]></category>
		<category><![CDATA[radius]]></category>

		<guid isPermaLink="false">http://bluehua.org/?p=1327</guid>
		<description><![CDATA[做前端的同学都应该听说或者用过，是一段脚本，可以让ie实现css3里的圆角和阴影效果：http://fetchak.com/ie-css3/ 用法大致如下 .box &#123; -moz-border-radius: 15px; /* Firefox */ -webkit-border-radius: 15px; /* Safari and Chrome */ border-radius: 15px; /* Opera 10.5+, future browsers, and now also Internet Explorer 6+ using IE-CSS3 */ &#160; -moz-box-shadow: 10px 10px 20px #000; /* Firefox */ -webkit-box-shadow: 10px 10px 20px #000; /* Safari and Chrome */ box-shadow: 10px 10px 20px [...]]]></description>
			<content:encoded><![CDATA[<p>做前端的同学都应该听说或者用过，是一段脚本，可以让ie实现css3里的圆角和阴影效果：<a href="http://fetchak.com/ie-css3/">http://fetchak.com/ie-css3/</a></p>
<p>用法大致如下</p>

<div class="wp_syntax"><div class="code overflow"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.box</span> <span style="color: #00AA00;">&#123;</span>
  -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">15px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* Firefox */</span>
  -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">15px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* Safari and Chrome */</span>
  border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">15px</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* Opera 10.5+, future browsers, and now also Internet Explorer 6+ using IE-CSS3 */</span>
&nbsp;
  -moz-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span> <span style="color: #933;">20px</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* Firefox */</span>
  -webkit-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span> <span style="color: #933;">20px</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* Safari and Chrome */</span>
  box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span> <span style="color: #933;">20px</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* Opera 10.5+, future browsers and IE6+ using IE-CSS3 */</span>
&nbsp;
  behavior<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>ie-css3.htc<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* This lets IE know to call the script on all elements which get the 'box' class */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>最近用到了这个东西，发现动态改变div的内容之后，这段脚本生成的vml会出现变形。。<br />
所以加了一个手动刷新的函数，通过innerHTML赋值之后调用一下就可以了</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;">el.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'....'</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">update_css3_fix</span><span style="color: #009900;">&#41;</span> update_css3_fix<span style="color: #009900;">&#40;</span>el<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>如果使用jquery就不用这么麻烦，在你的框架里加一段</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>jQuery.<span style="color: #660066;">browser</span>.<span style="color: #660066;">msie</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    jQuery.<span style="color: #660066;">fn</span>.__ohtml__ <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">html</span><span style="color: #339933;">;</span>
    jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">html</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        jQuery<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.__ohtml__<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            update_css3_fix<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>另外官网下载的脚本还会产生yourdomain/none的404请求，也已经修复</p>
<p>修改之后的文件<a href='http://bluehua.org/wp-content/uploads/2010/07/ie-css3.htc.zip'>ie-css3.htc</a></p>
]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2010/07/04/1327.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>js中简单实现数据和ui绑定</title>
		<link>http://bluehua.org/2010/05/18/1286.html</link>
		<comments>http://bluehua.org/2010/05/18/1286.html#comments</comments>
		<pubDate>Tue, 18 May 2010 09:27:44 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://bluehua.org/?p=1286</guid>
		<description><![CDATA[很简单的例子 //计数加一 count ++ //更新ui jQuery&#40;'#count'&#41;.text&#40;count&#41;; 相信几乎所有前端工程师都会写过上面这样的代码，更新数据，然后更新ui，行云流水般的。 然而也是相当一部分内测bug的起源，数据删除了，ui里的计数没有变，或者ui变了，数据里确没有删除。而在逻辑复杂的应用里，更是很难保证不出这种低级错误。 其实我们可以通过自定义事件，这样重写 this.data_bind&#40;'count', 'change', function&#40;count&#41; &#123; //更新计数 jQuery&#40;'#count'&#41;.text&#40;count&#41;; &#125;&#41;; .... &#160; this.data_bind&#40;'count', 'change', function&#40;count&#41; &#123; //刷新分页 refresh_pager&#40;count&#41;; &#125;&#41;; &#160; this.set_data&#40;'count', this.get_data&#40;'count'&#41; + 1&#41;; 上次D2上听金大为讲模板技术，当时感觉耳目一新，最近边写边考虑这些新思想，其实使用现有的自定义事件要简单的多，而且灵活性，适用性更好。 用过flex的同学看完这篇文章肯定会说：靠，你们写js的还真是原始社会。。]]></description>
			<content:encoded><![CDATA[<p>很简单的例子</p>

<div class="wp_syntax"><div class="code overflow"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">//</span>计数加一
count ++
<span style="color: #000000; font-weight: bold;">//</span>更新ui
jQuery<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'#count'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.text<span style="color: #7a0874; font-weight: bold;">&#40;</span>count<span style="color: #7a0874; font-weight: bold;">&#41;</span>;</pre></div></div>

<p>相信几乎所有前端工程师都会写过上面这样的代码，更新数据，然后更新ui，行云流水般的。 然而也是相当一部分内测bug的起源，数据删除了，ui里的计数没有变，或者ui变了，数据里确没有删除。而在逻辑复杂的应用里，更是很难保证不出这种低级错误。</p>
<p>其实我们可以通过自定义事件，这样重写</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">data_bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'change'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>count<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//更新计数</span>
    jQuery<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#count'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
....
&nbsp;
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">data_bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'change'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>count<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//刷新分页</span>
    refresh_pager<span style="color: #009900;">&#40;</span>count<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set_data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'count'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get_data</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'count'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>上次D2上听金大为讲模板技术，当时感觉耳目一新，最近边写边考虑这些新思想，其实使用现有的自定义事件要简单的多，而且灵活性，适用性更好。</p>
<p>用过flex的同学看完这篇文章肯定会说：靠，你们写js的还真是原始社会。。</p>
]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2010/05/18/1286.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>js业务框架中对ajax的再次包装</title>
		<link>http://bluehua.org/2010/05/10/1216.html</link>
		<comments>http://bluehua.org/2010/05/10/1216.html#comments</comments>
		<pubDate>Mon, 10 May 2010 08:30:20 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[web dev]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://bluehua.org/?p=1216</guid>
		<description><![CDATA[一个简单的load动作，如果简单用任何一个js框架实现 startloding&#40;&#41;; new ajax&#40;&#123; url : 'test.php', compete : function&#40;&#41; &#123; stoploading&#40;&#41; &#125;, success : function&#40;data&#41; &#123; try &#123; var obj = json_decode&#40;data&#41;; &#125; catch&#40;e&#41; &#123; //服务异常，未返回json造成异常 error&#40;'服务器异常'&#41;; return; &#125; &#160; //正常的业务逻辑错误 if &#40; json&#91;'code'&#93; !== 0 &#41; &#123; error&#40;json&#91;'desc'&#93;&#41;; return; &#125; &#160; ... &#125;, error : function&#40;&#41; &#123; //网络请求失败造成异常 error&#40;'网络异常'&#41;; &#125; &#125;&#41;; 而一个业务框架对ajax再次包装，成为一个action new [...]]]></description>
			<content:encoded><![CDATA[<p>一个简单的load动作，如果简单用任何一个js框架实现</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;">startloding<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">new</span> ajax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    url <span style="color: #339933;">:</span> <span style="color: #3366CC;">'test.php'</span><span style="color: #339933;">,</span>
    compete <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
         stoploading<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    success <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> json_decode<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #006600; font-style: italic;">//服务异常，未返回json造成异常</span>
            error<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'服务器异常'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">//正常的业务逻辑错误</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> json<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'code'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            error<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'desc'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        ...
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    error <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//网络请求失败造成异常</span>
        error<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'网络异常'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>而一个业务框架对ajax再次包装，成为一个action</p>

<div class="wp_syntax"><div class="code overflow"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">new</span> action<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    action <span style="color: #339933;">:</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span>
    action_name <span style="color: #339933;">:</span> <span style="color: #3366CC;">'加载'</span><span style="color: #339933;">,</span>
    url <span style="color: #339933;">:</span> <span style="color: #3366CC;">'test.php'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
api.<span style="color: #660066;">add_action</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'load_start'</span><span style="color: #339933;">,</span> startloading<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
api.<span style="color: #660066;">add_action</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'load_complete'</span><span style="color: #339933;">,</span> stoploading<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//这里的成功不是ajax成功，而是判定json['code']之后action真正的成功，这就要求所有action需要有统一的成功判定标准，和json包装格式</span>
api.<span style="color: #660066;">add_action</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'load_success'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>msg<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>  .... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//这里没有添加异常处理，因为没有特殊需要，所以action会执行默认的错误处理，可能是一个弹窗</span></pre></div></div>

<p>不仅仅是节省了代码，后者有更好的可读性，load动作本身也具有了更好的重用性</p>
<p>具体的框架代码已经在用，还不是很完美，待贴～</p>
]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2010/05/10/1216.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php的一个表单验证类</title>
		<link>http://bluehua.org/2010/04/23/1150.html</link>
		<comments>http://bluehua.org/2010/04/23/1150.html#comments</comments>
		<pubDate>Fri, 23 Apr 2010 15:19:36 +0000</pubDate>
		<dc:creator>冥王星2011</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[web dev]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[validate]]></category>

		<guid isPermaLink="false">http://bluehua.org/?p=1150</guid>
		<description><![CDATA[做web这么长时间,感觉最恶心的就是表单验证这块了,很无聊,很烦琐,也很重要.. 这个验证类的功能参考了codeigniter的表单验证库的功能,按照我的想法实现完毕又去对照codeigniter的代码,感觉还是我的这个比较灵活实用&#8230; /** * 表单验证类 * Author : emptyhua@gmail.com * Version : beta */ &#160; class FormValidator &#123; //所有验证函数定义时的前缀 const PREFIX = 'form_validator_'; &#160; //存储验证函数的出错信息 public static $error_messages = array&#40;&#41;; &#160; //存储验证相关的数据 protected $valid_rules = array&#40;&#41;; &#160; //存储上次验证的出错信息 protected $errors = array&#40;&#41;; &#160; //是否完全检查 protected $complete_check = true; &#160; public static function set_error&#40;$rule, $error&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>做web这么长时间,感觉最恶心的就是表单验证这块了,很无聊,很烦琐,也很重要..<br />
这个验证类的功能参考了<a href="http://codeigniter.org.cn/user_guide/libraries/form_validation.html">codeigniter的表单验证库</a>的功能,按照我的想法实现完毕又去对照codeigniter的代码,感觉还是我的这个比较灵活实用<img class="xemotion" src="http://bluehua.org/wp-content/plugins/x-emotions/emotions/msn/bofu1_12.gif" border="0" alt="" />&#8230;</p>

<div class="wp_syntax"><div class="code overflow"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * 表单验证类
 * Author : emptyhua@gmail.com
 * Version : beta
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FormValidator
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//所有验证函数定义时的前缀</span>
    <span style="color: #000000; font-weight: bold;">const</span> PREFIX <span style="color: #339933;">=</span> <span style="color: #0000ff;">'form_validator_'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//存储验证函数的出错信息</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000088;">$error_messages</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//存储验证相关的数据</span>
    protected <span style="color: #000088;">$valid_rules</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">//存储上次验证的出错信息</span>
    protected <span style="color: #000088;">$errors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//是否完全检查</span>
    protected <span style="color: #000088;">$complete_check</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> set_error<span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #339933;">,</span> <span style="color: #000088;">$error</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$error_messages</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$error</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * 遇到第一个错误便返回
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> first_error_only<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">complete_check</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * 执行所有验证规则,收集所有错误
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> show_all_errors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">complete_check</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * 设置验证规则
     * @param string $var 验证的变量名,例如 username
     * @param string $var_name 变量名的描述,例如 用户名
     * @param string $func_name 验证规则,可以是多个,并且可以传参,例如 require|length(9,12)
     * @param string $error_message 自定义的出错信息,可选,如果给定将覆盖验证函数的默认出错信息
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> set_rules<span style="color: #009900;">&#40;</span><span style="color: #000088;">$var</span><span style="color: #339933;">,</span> <span style="color: #000088;">$var_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$func_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$error_message</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'var_name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$var_name</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$error_message</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'funcs'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$funcs_with_arg</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'|'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$func_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$args_match</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//解析验证规则</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$funcs_with_arg</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$func_with_arg</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\((.*)\)$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$func_with_arg</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args_match</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #666666; font-style: italic;">//规则函数是否携带参数</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args_match</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//参数列表</span>
                <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args_match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">//去掉参数后的验证规则名</span>
                <span style="color: #000088;">$func</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$args_match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">')'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$func_with_arg</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'funcs'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$func</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$args</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">else</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'funcs'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$func_with_arg</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid_rules</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * 检验一组数据是否合法
     * @param array $data 一个关联数组
     * @return boolean
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> is_valid<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$valid</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//遍历的顺序为规则添加的顺序</span>
        <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid_rules</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$var</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$rule</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//判断数据中是否有此字段,没有的话置一个空的</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
                <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rule</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'funcs'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$func</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">//真实的验证函数的名称,例如 form_validator_require</span>
                <span style="color: #000088;">$func_run</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">PREFIX</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$func</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">//如果函数未定义,循环继续</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_callable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$func_run</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">//为验证函数组装参数</span>
                <span style="color: #666666; font-style: italic;">//顺序为:当前验证字段的值, 所有验证数据, 验证规则参数1, ...</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$args</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">NULL</span> <span style="color: #009900;">&#41;</span> 
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #990000;">array_unshift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #990000;">array_unshift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$var</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">//调用验证函数</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$func_run</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$valid</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                    <span style="color: #666666; font-style: italic;">//如果set_rules的时候没有自定义错误信息,则使用默认出错信息</span>
                    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$rule</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">NULL</span> <span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">//使用sprintf拼接错误信息</span>
                        <span style="color: #666666; font-style: italic;">//参数顺序: 验证字段的描述, 验证函数的参数1, 验证函数的参数2 ...</span>
&nbsp;
                        <span style="color: #666666; font-style: italic;">//去掉刚才添加的字段值</span>
                        <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #666666; font-style: italic;">//去掉刚才添加的所有验证数据</span>
                        <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #666666; font-style: italic;">//添加字段名的描述</span>
                        <span style="color: #990000;">array_unshift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rule</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'var_name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #666666; font-style: italic;">//添加默认的出错信息</span>
                        <span style="color: #990000;">array_unshift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$error_messages</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$func</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">call_user_func_array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sprintf'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    <span style="color: #b1b100;">else</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">//使用自定义错误信息</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rule</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">continue</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
&nbsp;
                    <span style="color: #666666; font-style: italic;">//如果设置不进行完全检查,则发现第一个错误后跳出最外层循环</span>
                    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">complete_check</span> <span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">break</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span> 
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$valid</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * 获取出错信息
     * @return string array
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_errors<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * 返回第一条出错信息
     * @return string
     */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_first_error<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errors</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//扩展验证规则很方便~</span>
<span style="color: #666666; font-style: italic;">//设置规则的默认出错信息</span>
FormValidator<span style="color: #339933;">::</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'require'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'%s不能为空'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//定义验证函数,以FormValidator::PREFIX打头</span>
<span style="color: #000000; font-weight: bold;">function</span> form_validator_require<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
FormValidator<span style="color: #339933;">::</span><span style="color: #004000;">set_error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'equal'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'重复输入不一致'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> form_validator_equal<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$eqto</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$eqto</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$eqto</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$str</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>使用方法</p>

<div class="wp_syntax"><div class="code overflow"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$vd</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FormValidator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$vd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rules</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'p'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'密码'</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">'require|length(9,12)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$vd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_rules</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'p2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'密码验证'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'equa(p)'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'两次密码输入不一致'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$vd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">is_valid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$vd</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_errors</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://bluehua.org/2010/04/23/1150.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.086 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-05-22 08:08:21 -->

