<?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; jquery</title>
	<atom:link href="http://wp.serpere.info/archives/tag/jquery/feed" rel="self" type="application/rss+xml" />
	<link>http://wp.serpere.info</link>
	<description>樹上で暮らすヘビのように生きたい</description>
	<lastBuildDate>Mon, 06 Sep 2010 07:43:34 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>jQuery History is now on Github</title>
		<link>http://wp.serpere.info/archives/1234</link>
		<comments>http://wp.serpere.info/archives/1234#comments</comments>
		<pubDate>Sat, 08 May 2010 12:47:14 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jQuery History]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1234</guid>
		<description><![CDATA[I created a fork of jQuery History plugin on Github. http://github.com/tkyk/jquery-history-plugin (the latest source code) Issue Tracking System: http://github.com/tkyk/jquery-history-plugin/issues Test page: http://www.serpere.info/jquery-history-plugin/sample/ Though this is just a fork for now, Mikage Sawatari, the original author of this plugin, and I agreed that it could be the mainstream in the near future. I&#8217;ve already done some [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1234%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2Fc95nCZ%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22jQuery%20History%20is%20now%20on%20Github%22%20%7D);"></div>
<div id="jquery-history-en">
<p>I created a fork of jQuery History plugin on Github.</p>
<ul>
	<li><a href="http://github.com/tkyk/jquery-history-plugin">http://github.com/tkyk/jquery-history-plugin</a> (<a href="http://github.com/tkyk/jquery-history-plugin/raw/master/jquery.history.js">the latest source code</a>)</li>
	<li>Issue Tracking System: <a href="http://github.com/tkyk/jquery-history-plugin/issues">http://github.com/tkyk/jquery-history-plugin/issues</a></li>
	<li>Test page: <a href="http://www.serpere.info/jquery-history-plugin/sample/">http://www.serpere.info/jquery-history-plugin/sample/</a></li>
</ul>
<p>Though this is just a fork for now, Mikage Sawatari, the original author of this plugin, and I agreed that it could be the mainstream in the near future.</p>
<p>I&#8217;ve already done some major changes to the script structure:</p>
<ul>
	<li>It no longer uses the Webkit-specific hack for updating the location object, because recent versions of Webkit can correctly update the object just like Firefix and IE8.</li>
	<li>Hashes are percent-encoded with encodeURIComponent before being put to location.hash. So you can use any characters such as `?&#8217; in your hash.</li>
	<li>history.load does not execute the callback unless hash is changed.</li>
	<li>Updated API/code format. Thanks to <a href="http://www.mikage.to/jquery/history/rbbs.cgi?id=RA12619431290839983526&amp;focus=1">Kevin Dalman</a>.</li>
</ul>
<p>It works on IE6, IE7, IE8, Firefox3, Safari4, Chrome4, and Chrome5.</p>
<p>If you have any problems or feature requests, please create a new issue in the <a href="http://github.com/tkyk/jquery-history-plugin/issues">ITS</a>. And of course, you can create your own fork.</p>
</div>
<hr />
<div id="jquery-history-ja">
<p>jQuery Historyプラグインは今後Githubで管理されることになりました。</p>
<ul>
<li>プロジェクトページ: <a href="http://github.com/tkyk/jquery-history-plugin">http://github.com/tkyk/jquery-history-plugin</a> (<a href="http://github.com/tkyk/jquery-history-plugin/raw/master/jquery.history.js">最新版のソースコード</a>)</li>
<li>バグトラッキングシステム: <a href="http://github.com/tkyk/jquery-history-plugin/issues">http://github.com/tkyk/jquery-history-plugin/issues</a></li>
<li>動作サンプル: <a href="http://www.serpere.info/jquery-history-plugin/sample/">http://www.serpere.info/jquery-history-plugin/sample/</a></li>
</ul>
<p>Mikage Sawatariさんによる元々のバージョンに対し、次のような大きな変更が加えられています。</p>
<ul>
<li>Webkitで動かすためのハックは不要になったので取り除かれました。</li>
<li>location.hash に代入する前に encodeURIComponent によるパーセントエンコーディングを行うようにしました。これによって ? を含むあらゆる文字を hash として保存できるようになりました。</li>
<li>ハッシュが変更されていない場合はコールバックが実行されないようになりました。</li>
<li>API/コーディングスタイルを変更しました。この変更はフォーラムの<a href="http://www.mikage.to/jquery/history/rbbs.cgi?id=RA12619431290839983526&amp;focus=1">Kevin Dalman氏の投稿</a>に依ります。</li>
</ul>
<p>IE6, IE7, IE8, Firefox3, Safari4, Chrome4, Chrome5で動作します。</p>
<p><a href="http://github.com/tkyk/jquery-history-plugin/issues">Githubのバグトラッキングシステム</a>に登録してください。もちろん自分でforkを作って修正していただいても構いません。</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1234/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Cicindelaを使ったAjaxアプリケーションサンプル 2</title>
		<link>http://wp.serpere.info/archives/1103</link>
		<comments>http://wp.serpere.info/archives/1103#comments</comments>
		<pubDate>Sat, 09 Jan 2010 13:42:44 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[cicindela]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1103</guid>
		<description><![CDATA[ライブドア製レコメンデーション・エンジン Cicindela の動作を確かめるために、簡単なAjaxアプリケーションを作ってみました。 前回の続きです。 JavaScript: Logger Cicindela との連携部分を作る前に、簡単な Logger を作っておきます。ログの出力先には TEXTAREA 要素を想定しています。 var Logger = &#123; clear: function&#40;&#41; &#123; $&#40;'#log'&#41;.val&#40;&#34;&#34;&#41;; &#125;, append: function&#40;text&#41; &#123; var dom = $&#40;'#log'&#41;.get&#40;0&#41;; dom.value+= text + &#34;\n&#34;; dom.scrollTop = dom.scrollHeight; &#125; &#125;; Cicindela への入力 まずは入力部分から作っていきます。このアプリケーションにおいて必要な操作は「あるユーザがアイテムを選択した」、「あるアイテムをあるカテゴリに所属させる」の2つです。これらの操作を抽象化するために、Cicindela Web APIに対する Proxy となるオブジェクトを作ります。 var Cicindela = &#123; base: '/cicindela/', set: 'sample', _insert_op_url: function&#40;op, item&#41; [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1103%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Cicindela%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9FAjax%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%202%22%20%7D);"></div>
<p>
ライブドア製レコメンデーション・エンジン <a href="http://labs.edge.jp/cicindela/">Cicindela</a> の動作を確かめるために、簡単なAjaxアプリケーションを作ってみました。
</p>
<p>
<a href="http://wp.serpere.info/archives/1101">前回</a>の続きです。
</p>
<h3>JavaScript: Logger</h3>
<p>
Cicindela との連携部分を作る前に、簡単な Logger を作っておきます。ログの出力先には TEXTAREA 要素を想定しています。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Logger <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    clear<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: #009900;">&#40;</span><span style="color: #3366CC;">'#log'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    append<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> dom <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#log'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        dom.<span style="color: #660066;">value</span><span style="color: #339933;">+=</span> text <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        dom.<span style="color: #660066;">scrollTop</span> <span style="color: #339933;">=</span> dom.<span style="color: #660066;">scrollHeight</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>



<h3>Cicindela への入力</h3>
<p>
まずは入力部分から作っていきます。このアプリケーションにおいて必要な操作は「<a href="http://code.google.com/p/cicindela2/wiki/WebAPI#%E3%80%8C%E3%81%82%E3%82%8B%E3%83%A6%E3%83%BC%E3%82%B6%E3%81%8C%E3%81%82%E3%82%8B%E3%82%A2%E3%82%A4%E3%83%86%E3%83%A0%E3%82%92%E9%81%B8%E6%8A%9E%E3%81%97%E3%81%9F%E3%80%8D">あるユーザがアイテムを選択した</a>」、「<a href="http://code.google.com/p/cicindela2/wiki/WebAPI#%E3%80%8C%E3%81%82%E3%82%8B%E3%82%A2%E3%82%A4%E3%83%86%E3%83%A0%E3%82%92%E3%81%82%E3%82%8B%E3%82%AB%E3%83%86%E3%82%B4%E3%83%AA%E3%81%AB%E6%89%80%E5%B1%9E%E3%81%95%E3%81%9B%E3%82%8B%E3%80%8D">あるアイテムをあるカテゴリに所属させる</a>」の2つです。これらの操作を抽象化するために、Cicindela Web APIに対する Proxy となるオブジェクトを作ります。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Cicindela <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    base<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/cicindela/'</span><span style="color: #339933;">,</span>
    set<span style="color: #339933;">:</span>  <span style="color: #3366CC;">'sample'</span><span style="color: #339933;">,</span>
    _insert_op_url<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>op<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">base</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;record?set=&quot;</span><span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&amp;item_id=&quot;</span><span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;op=&quot;</span><span style="color: #339933;">+</span> op<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    _send_req<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> cb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Logger.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;send Request to &quot;</span> <span style="color: #339933;">+</span> url<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> type<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
                 url<span style="color: #339933;">:</span> url<span style="color: #339933;">,</span>
                 success<span style="color: #339933;">:</span> cb<span style="color: #339933;">,</span>
                 complete<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>xhr<span style="color: #339933;">,</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                     Logger.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;receive Response[&quot;</span><span style="color: #339933;">+</span> xhr.<span style="color: #000066;">status</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;] from &quot;</span><span style="color: #339933;">+</span> url<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>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    set_category<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> category<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._send_req<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._insert_op_url<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;set_category&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;category_id=&quot;</span><span style="color: #339933;">+</span> category<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    insert<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>user<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._send_req<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>._insert_op_url<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;insert_pick&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;user_id=&quot;</span><span style="color: #339933;">+</span> user<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: #339933;">;</span></pre></div></div>



<p>
insert がアイテム選択、set_category がカテゴリ登録のためのメソッドです。実際にリクエスト送信を行うのは _send_req メソッドで、送信先URLと成功時に呼び出されるコールバック関数とを引数にとります。set プロパティには集計セットの名前を書いておいてください。
</p>
<p>
あとは「insert」ボタンが押されたときに、これらの API を呼び出します。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// $(document).ready において</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button#insert'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span>
      <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> user_id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#username'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>user_id <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>category_name<span style="color: #339933;">,</span> category_data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                   <span style="color: #003366; font-weight: bold;">var</span> category_id <span style="color: #339933;">=</span> category_data.<span style="color: #660066;">id</span><span style="color: #339933;">;</span>
                   <span style="color: #003366; font-weight: bold;">var</span> container <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #339933;">+</span> category_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                   <span style="color: #003366; font-weight: bold;">var</span> values <span style="color: #339933;">=</span> container.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                   $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>values<span style="color: #339933;">,</span>
                          <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                              Cicindela.<span style="color: #660066;">set_category</span><span style="color: #009900;">&#40;</span>v<span style="color: #339933;">,</span> category_id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                              Cicindela.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span>user_id<span style="color: #339933;">,</span> v<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                              $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#username'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&quot;</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: #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: #003366; font-weight: bold;">false</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>
このアプリケーションの場合、カテゴリ登録はデータの初期化時にまとめて行った方が効率的なのですが、「同じアイテムを、何度同じカテゴリに登録しても問題ない」ことを確かめるために、あえてアイテム選択時に毎回登録を実行しています。
</p>
<p>
これで Cicindela への入力準備が整いました。ユーザ名を入力して、適当にアイテムを選択して「insert」ボタンを押してみます。成功すれば次のようなログが出力されるはずです。
</p>
<pre>
send Request to /cicindela/record?set=sample&#038;item_id=Ruby&#038;op=set_category&#038;category_id=1
send Request to /cicindela/record?set=sample&#038;item_id=Ruby&#038;op=insert_pick&#038;user_id=cicindela
receive Response[204] from /cicindela/record?set=sample&#038;item_id=Ruby&#038;op=insert_pick&#038;user_id=cicindela
receive Response[204] from /cicindela/record?set=sample&#038;item_id=Ruby&#038;op=set_category&#038;category_id=1
</pre>
<p>
またMySQLサーバにログインして、categories テーブルや picks テーブルにデータが入っていることを確認します。
</p>
<h3>Cicindela からの出力</h3>
<p>
続いて Cicindela からレコメンデーション・データを取り出してみます。必要なのは「<a href="http://code.google.com/p/cicindela2/wiki/WebAPI#%E7%89%B9%E5%AE%9A%E3%82%A2%E3%82%A4%E3%83%86%E3%83%A0%E3%81%AB%E5%AF%BE%E3%81%99%E3%82%8B%E3%83%AC%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%87%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%28%E9%96%A2%E9%80%A3">特定アイテムに対するレコメンデーション(関連アイテム)取得 (=item to item)</a>」なので、次のような get_for_item メソッドを Cicindela オブジェクトに追加します。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Cicindela <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009966; font-style: italic;">/* ...略... */</span>
&nbsp;
    get_for_item<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #339933;">,</span> category<span style="color: #339933;">,</span> cb<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> url <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">base</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">'recommend?set='</span><span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">set</span>
            <span style="color: #339933;">+</span><span style="color: #3366CC;">'&amp;op=for_item&amp;item_id='</span><span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;&amp;category_id=&quot;</span><span style="color: #339933;">+</span> category<span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>._send_req<span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> cb<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: #339933;">;</span></pre></div></div>



<p>
そして「get」ボタンが押されたときにこのメソッドを呼び出します。第3引数として渡すコールバック関数は、正常にレスポンスが受信できたときに受信データを引数として呼び出されます。Cicindela からのレスポンスは改行区切りの文字列データです。ここでは単に出力用の要素に html メソッドで書き込んでおきます。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// $(document).ready において</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#get'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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>
          <span style="color: #003366; font-weight: bold;">var</span> item_id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#items'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>category_name<span style="color: #339933;">,</span> category_data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                     Cicindela.<span style="color: #660066;">get_for_item</span><span style="color: #009900;">&#40;</span>item_id<span style="color: #339933;">,</span>
                                            category_data.<span style="color: #660066;">id</span><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: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#output-&quot;</span><span style="color: #339933;">+</span> category_name<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<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: #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>
以上で一通りの機能が完成しました。ユーザ名と選択アイテムを変えて何度か入力を行い、レコメンデーションを取得してみましょう。その際には自分なりに”シナリオ”を決めて、そのシナリオに沿った結果になるか試してみると面白いです。例えば：「Perl好きはEmacsやVimを使う」、「WindowsユーザにもMacユーザにもLinuxユーザにもJava好きはいる」、「EmacsとVimを両方ともよく使うユーザは少ない」などなど。
</p>
<p>
完全なソースを gist に置いておきます。
</p>
<ul>
<li><a href="http://gist.github.com/272913">cicindela.html</a></li>
<li><a href="http://gist.github.com/272910">cicindela-sample.js</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1103/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cicindelaを使ったAjaxアプリケーションサンプル</title>
		<link>http://wp.serpere.info/archives/1101</link>
		<comments>http://wp.serpere.info/archives/1101#comments</comments>
		<pubDate>Thu, 07 Jan 2010 12:51:36 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[プログラミング]]></category>
		<category><![CDATA[cicindela]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1101</guid>
		<description><![CDATA[ライブドア製レコメンデーション・エンジン Cicindela の動作を確かめるために、簡単なAjaxアプリケーションを作ってみました。スクリーンショット： 「好きなプログラミング言語」と「よく使うソフトウェア」についての情報を収集し、その結果に基づいておすすめの言語・ソフトを算出する、一種のアンケートです。 現実にはAjaxアプリケーションから直接 Cicindela のWeb APIを叩くのは難しいと思いますが（いたずらされ放題になってしまうので）、機能をテストするのには手軽です。以下に作り方を紹介しておきます。 Cicindela はセットアップ済みとします（CentOS 5にセットアップする手順はこちら） カテゴリー機能を使います アイテムid・ユーザidは文字列です jQuery 1.3.2 を使います Firefox 3で動作確認します（ほかのブラウザでも動くと思いますが） データベースの初期化 create_init_sql.pl を実行して使用するデータベースを作成します。DB名は sample としました。 perl create_init_sql.pl --db_name=sample &#124; mysql -uroot cicindelaの設定 lib/Cicindela/Config/_common.pm に集計セットを定義します。 'sample' =&#62; &#123; datasource =&#62; &#91; 'dbi:mysql:sample;host=localhost', 'cicindela', 'cicindela' &#93;, filters =&#62; &#91; 'PicksExtractor', 'InverseUserFrequency', 'ItemSimilarities', &#93;, recommender =&#62; 'ItemSimilarities::LimitCategory', refresh_interval =&#62; 1, [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1101%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Cicindela%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9FAjax%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%22%20%7D);"></div>
<p>
ライブドア製レコメンデーション・エンジン <a href="http://labs.edge.jp/cicindela/">Cicindela</a> の動作を確かめるために、簡単なAjaxアプリケーションを作ってみました。スクリーンショット：
</p>
<p>
<a href="http://wp.serpere.info/wp-content/uploads/2010/01/cicindela.gif"><img src="http://wp.serpere.info/wp-content/uploads/2010/01/cicindela-300x266.gif" alt="Cicindela Ajaxアプリ" title="Cicindela Ajaxアプリ" width="300" height="266" class="size-medium wp-image-1102" /></a>
</p>
<p>
「好きなプログラミング言語」と「よく使うソフトウェア」についての情報を収集し、その結果に基づいておすすめの言語・ソフトを算出する、一種のアンケートです。
</p>
<p>
現実にはAjaxアプリケーションから直接 Cicindela のWeb APIを叩くのは難しいと思いますが（いたずらされ放題になってしまうので）、機能をテストするのには手軽です。以下に作り方を紹介しておきます。
</p>
<ul>
<li>Cicindela はセットアップ済みとします（<a href="http://wp.serpere.info/archives/1100">CentOS 5にセットアップする手順はこちら</a>）</li>
<li>カテゴリー機能を使います</li>
<li>アイテムid・ユーザidは文字列です</li>
<li>jQuery 1.3.2 を使います</li>
<li>Firefox 3で動作確認します（ほかのブラウザでも動くと思いますが）</li>
</ul>
<h3>データベースの初期化</h3>
<p>
create_init_sql.pl を実行して使用するデータベースを作成します。DB名は sample としました。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> create_init_sql.pl --db_name=sample <span style="color: #000000; font-weight: bold;">|</span> mysql <span style="color: #660033;">-uroot</span></pre></div></div>



<h3>cicindelaの設定</h3>
<p>
lib/Cicindela/Config/_common.pm に集計セットを定義します。
</p>


<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;">    <span style="color: #ff0000;">'sample'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span>
        datasource <span style="color: #339933;">=&gt;</span>  <span style="color: #009900;">&#91;</span> <span style="color: #ff0000;">'dbi:mysql:sample;host=localhost'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'cicindela'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'cicindela'</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        filters <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#91;</span>
            <span style="color: #ff0000;">'PicksExtractor'</span><span style="color: #339933;">,</span>
            <span style="color: #ff0000;">'InverseUserFrequency'</span><span style="color: #339933;">,</span>
            <span style="color: #ff0000;">'ItemSimilarities'</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        recommender <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'ItemSimilarities::LimitCategory'</span><span style="color: #339933;">,</span>
        refresh_interval <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
&nbsp;
        use_user_char_id <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        use_item_char_id <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
        discard_user_id_char2int <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'6 month'</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>



<p>
カテゴリ機能を使いたいので、フィルタとして ItemSimilarities を、レコメンダとして ItemSimilarities::LimitCategory を指定します。またユーザidとアイテムidに文字列を使用したいので、 use_*_char_id を設定します。結果を確認しやすくするために refresh_interval を 1 にしていますが、実運用時にはもっと大きな値を指定してください。
</p>
<h3>HTML</h3>
<p>
HTMLのbody内を抜粋すると次のようになります。SELECT 要素は後からJavaScriptで初期化するので、この段階では空っぽです。
</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>追加<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;insert&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>username: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;username&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>好きな言語<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;select</span> <span style="color: #000066;">multiple</span>=<span style="color: #ff0000;">&quot;multiple&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;langs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/select<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>よく使うソフト<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;select</span> <span style="color: #000066;">multiple</span>=<span style="color: #ff0000;">&quot;multiple&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;softs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/select<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;submit-space&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;insert&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>insert<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>取得<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;recommend&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;select</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;items&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/select<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>おすすめ言語<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;output-langs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>おすすめソフト<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/legend<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;output-softs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fieldset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;submit-space&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;button</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;get&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>get<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/button<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ログ<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/h2<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;textarea</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;log&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/textarea<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>




<h3>JavaScript: データの定義</h3>
<p>
使用するデータは JavaScript のオブジェクトとして、次のような構造で定義します。langs と softs という2つのカテゴリがあり、それぞれに数値のid（1,2）が割り当てられています。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    langs<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span>
             list<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Perl'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'PHP'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Ruby'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Python'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Java'</span><span style="color: #339933;">,</span>
                    <span style="color: #3366CC;">'JavaScript'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'CSharp'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Erlang'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'CommonLisp'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    softs<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
             list<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Windows'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Linux'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Mac'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Emacs'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Vim'</span><span style="color: #339933;">,</span>
                    <span style="color: #3366CC;">'Eclipse'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'NetBeans'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'VisualStudio'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'Dreamweaver'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>




<h3>JavaScript: SELECTの初期化</h3>
<p>
document の ready イベントにてSELECT要素を初期化します。Cicindela に渡す item_id としてアイテムの名前をそのまま使うので、OPTION要素の value 値にはアイテム名を設定しておきます。
</p>


<div class="wp_syntax"><div class="code"><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: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>category_name<span style="color: #339933;">,</span> category_data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                 <span style="color: #003366; font-weight: bold;">var</span> all_items <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#items'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #003366; font-weight: bold;">var</span> category_items <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span><span style="color: #339933;">+</span> category_name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>category_data.<span style="color: #660066;">list</span><span style="color: #339933;">,</span>
                        <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span>l<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;option /&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span> l<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>l<span style="color: #009900;">&#41;</span>
                                .<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>category_items<span style="color: #009900;">&#41;</span>
                                .<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>all_items<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: #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></pre></div></div>



<p>
続く。
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1101/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>動的にSELECTを構築するためのjQueryプラグイン</title>
		<link>http://wp.serpere.info/archives/1096</link>
		<comments>http://wp.serpere.info/archives/1096#comments</comments>
		<pubDate>Mon, 14 Dec 2009 14:43:48 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1096</guid>
		<description><![CDATA[全てのブラウザで安定してSELECT要素を操作するためのプラグイン……というか、実質IE対策プラグインです。 gits: 256077（動作テスト） ごく小さなプラグインで、余計な機能は一切ついていません。使用できるメソッドは clearOptions と addOption の2つです。当たり前ですが、SELECT要素以外に使っても意味はありません。 clearOptions()全てのOPTION要素を削除します addOption(text, value, selected)新しいOPTION要素を追加します。引数はそれぞれ表示されるテキスト、値、選択状態です こんな感じで使います。 //start月からend月までの選択肢を作る function updateMonth&#40;start, end&#41; &#123; var select = $&#40;'#month'&#41;; var current = select.val&#40;&#41;; &#160; select.clearOptions&#40;&#41;; for&#40;var i=start; i&#60;=end; ++i&#41; &#123; select.addOption&#40;i + &#34;月&#34;, i, i == current&#41; &#125; &#125; 全てのブラウザで（というかIEで）安定した結果を得るために、次の点に注意すると良いでしょう： 構築直後に val メソッドによる選択は行わない。代わりに addOption メソッドの第3引数でデフォルトの選択状態を設定する 構築処理全体を setTimeout で遅延実行する 検索で来る方のために、このプラグインを作るまでにIEで遭遇した問題も書いておきます： 構築後、SELECTの横幅がおかしくなる。構築を繰り返すたびにだんだんと短くなり、最後には消滅する 構築に時間がかかる。そのために処理の途中でSELECTの表示がチラつく 構築直後に [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1096%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22%E5%8B%95%E7%9A%84%E3%81%ABSELECT%E3%82%92%E6%A7%8B%E7%AF%89%E3%81%99%E3%82%8B%E3%81%9F%E3%82%81%E3%81%AEjQuery%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%22%20%7D);"></div>
<p>
全てのブラウザで安定してSELECT要素を操作するためのプラグイン……というか、実質IE対策プラグインです。
</p>
<p>
<a href="http://gist.github.com/256077">gits: 256077</a>（<a href="http://www.serpere.info/options/">動作テスト</a>）
</p>
<p>
ごく小さなプラグインで、余計な機能は一切ついていません。使用できるメソッドは clearOptions と addOption の2つです。当たり前ですが、SELECT要素以外に使っても意味はありません。
</p>
<dl>
<dt>clearOptions()</dt><dd>全てのOPTION要素を削除します</dd>
<dt><th>addOption(text, value, selected)</dt><dd>新しいOPTION要素を追加します。引数はそれぞれ表示されるテキスト、値、選択状態です</dd>
</dl>
<p>
こんな感じで使います。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//start月からend月までの選択肢を作る</span>
<span style="color: #003366; font-weight: bold;">function</span> updateMonth<span style="color: #009900;">&#40;</span>start<span style="color: #339933;">,</span> end<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> select  <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#month'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> current <span style="color: #339933;">=</span> select.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  select.<span style="color: #660066;">clearOptions</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span>start<span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;=</span>end<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      select.<span style="color: #660066;">addOption</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;月&quot;</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> i <span style="color: #339933;">==</span> current<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



<p>
全てのブラウザで（というかIEで）安定した結果を得るために、次の点に注意すると良いでしょう：
</p>
<ul>
<li>構築直後に val メソッドによる選択は行わない。代わりに addOption メソッドの第3引数でデフォルトの選択状態を設定する</li>
<li>構築処理全体を setTimeout で遅延実行する</li>
</ul>
<p>
検索で来る方のために、このプラグインを作るまでにIEで遭遇した問題も書いておきます：
</p>
<ul>
<li>構築後、SELECTの横幅がおかしくなる。構築を繰り返すたびにだんだんと短くなり、最後には消滅する</li>
<li>構築に時間がかかる。そのために処理の途中でSELECTの表示がチラつく</li>
<li>構築直後に val メソッドで選択状態を変更すると「selected プロパティを設定できませんでした。未定義のエラーです。」となる（val メソッドを使わず、addOption メソッドの第3引数を使うことで回避可能）</li>
<li>ページ読み込み直後にSELECTを構築すると、選択がレンダリング結果に反映されない（構築処理全体を setTimeout で遅延実行することで回避可能）</li>
</ul>
<p>
コード全体：
</p>
<script src="http://gist.github.com/256077.js?file=jquery.dynamicselect.js"></script>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1096/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQueryでPrototype.js風のクラスを使う</title>
		<link>http://wp.serpere.info/archives/1091</link>
		<comments>http://wp.serpere.info/archives/1091#comments</comments>
		<pubDate>Sat, 28 Nov 2009 13:52:22 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1091</guid>
		<description><![CDATA[Prototype.js 風の Class を書きたいが、ライブラリ全体は読み込みたくない、というときのために、必要最低限のコードを抜き出してみた。要jQuery。 var Class = &#40;function&#40;&#41; &#123; function subclass&#40;&#41; &#123;&#125; return &#123; create: function&#40;parent&#41; &#123; function klass&#40;&#41; &#123; this.initialize.apply&#40;this, arguments&#41;; &#125; &#160; var index = 0; if&#40;jQuery.isFunction&#40;parent&#41;&#41; &#123; index = 1; subclass.prototype = parent.prototype; klass.prototype = new subclass; &#125; for&#40;; index &#60; arguments.length; ++index&#41; &#123; jQuery.extend&#40;klass.prototype, arguments&#91;index&#93;&#41;; &#125; return klass; &#125; &#125; &#125;&#41;&#40;&#41;; [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F1091%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22jQuery%E3%81%A7Prototype.js%E9%A2%A8%E3%81%AE%E3%82%AF%E3%83%A9%E3%82%B9%E3%82%92%E4%BD%BF%E3%81%86%22%20%7D);"></div>
<p>Prototype.js 風の Class を書きたいが、ライブラリ全体は読み込みたくない、というときのために、必要最低限のコードを抜き出してみた。要jQuery。</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> <span style="color: #003366; font-weight: bold;">Class</span> <span style="color: #339933;">=</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>
        <span style="color: #003366; font-weight: bold;">function</span> subclass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span>
        create<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>parent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">function</span> klass<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;">this</span>.<span style="color: #660066;">initialize</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> arguments<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;">var</span> index <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>jQuery.<span style="color: #660066;">isFunction</span><span style="color: #009900;">&#40;</span>parent<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    index <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
                    subclass.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> parent.<span style="color: #660066;">prototype</span><span style="color: #339933;">;</span>
                    klass.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> subclass<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">;</span> index <span style="color: #339933;">&lt;</span> arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>index<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    jQuery.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>klass.<span style="color: #660066;">prototype</span><span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#91;</span>index<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: #000066; font-weight: bold;">return</span> klass<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;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>最大の違いは $super による親クラスのメソッド呼び出しができないこと。<a href="http://www.prototypejs.org/learn/class-inheritance">Prototype.jsのチュートリアルの例</a>は次のようになる。</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Person <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
  initialize<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>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</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>
  say<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">': '</span> <span style="color: #339933;">+</span> message<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>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Pirate <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #660066;">create</span><span style="color: #009900;">&#40;</span>Person<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
  say<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//親クラスのprototypeに直接アクセス</span>
    <span style="color: #000066; font-weight: bold;">return</span> Person.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">say</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> message<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">', yarr!'</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>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> john <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Pirate<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Long John'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
john.<span style="color: #660066;">say</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'ahoy matey'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// -&gt; &quot;Long John: ahoy matey, yarr!&quot;</span></pre></div></div>



<p>mix-in の例などはそのまま動く。</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1091/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery HistoryプラグインのIE8対応</title>
		<link>http://wp.serpere.info/archives/783</link>
		<comments>http://wp.serpere.info/archives/783#comments</comments>
		<pubDate>Thu, 08 Oct 2009 09:42:26 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jQuery History]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=783</guid>
		<description><![CDATA[jQuery History is now on Github. Please check it. jQuery Historyプラグインの最新版はGithubにあります。]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_jade" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fwp.serpere.info%252Farchives%252F783%22%2C%20%22shorturl%22%3A%20%22http%3A%2F%2Fbit.ly%2F9GJSou%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22jQuery%20History%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%AEIE8%E5%AF%BE%E5%BF%9C%22%20%7D);"></div>
<p>
<a href="http://wp.serpere.info/archives/1234">jQuery History is now on Github. Please check it.
</a></p>
<p>
<a href="http://wp.serpere.info/archives/1234#jquery-history-ja">jQuery Historyプラグインの最新版はGithubにあります。</a>
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/783/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
