<?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>へびにっき</title>
	<atom:link href="http://wp.serpere.info/feed" rel="self" type="application/rss+xml" />
	<link>http://wp.serpere.info</link>
	<description>樹上で暮らすヘビのように生きたい</description>
	<lastBuildDate>Mon, 08 Feb 2010 02:42:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CakePHPでSchemalessなデータを扱う</title>
		<link>http://wp.serpere.info/archives/1112</link>
		<comments>http://wp.serpere.info/archives/1112#comments</comments>
		<pubDate>Sun, 07 Feb 2010 13:47:29 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[CakePHP]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1112</guid>
		<description><![CDATA[

拙作のKeyValueSource（key-valueストアのためのDataSource）で、スキーマ・レスなデータを保存できるようにしてみました。


cakephp-key-value-source &#8211; GitHub
（このプロジェクトは実用より実験を重視しているので、頻繁に仕様が変わります）


モデルクラスで $looseSchema というプロパティを設定すると、プライマリキーidを除いて、どんな構造のデータでも保存できるようになります。



class SchemalessUser extends AppModel &#123;
  var $useDbConfig = 'memcache';
  var $looseSchema = true;
&#125;
&#160;
// in Controllers 
$SchemalessUser-&#62;save&#40;array&#40;'id' =&#62; 1234,
                            'name' =&#62; 'John Smith',
 [...]]]></description>
			<content:encoded><![CDATA[
<p>
拙作のKeyValueSource（key-valueストアのためのDataSource）で、スキーマ・レスなデータを保存できるようにしてみました。
</p>
<p>
<a href="http://github.com/tkyk/cakephp-key-value-source">cakephp-key-value-source &#8211; GitHub</a><br />
（このプロジェクトは実用より実験を重視しているので、頻繁に仕様が変わります）
</p>
<p>
モデルクラスで $looseSchema というプロパティを設定すると、プライマリキーidを除いて、どんな構造のデータでも保存できるようになります。
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> SchemalessUser <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useDbConfig</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'memcache'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$looseSchema</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: #666666; font-style: italic;">// in Controllers </span>
<span style="color: #000088;">$SchemalessUser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1234</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'hobby'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'baseball'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'soccer'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$SchemalessUser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1235</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'firstname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John'</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'lasttname'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Smith'</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>
saveメソッドの fieldList オプションでキーを制限することができます。
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$SchemalessUser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1234</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">'hobby'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'baseball'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'soccer'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                      <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fieldList'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// hobby は保存されない</span>
<span style="color: #666666; font-style: italic;">// array('id' =&gt; 1234, 'name' =&gt; 'John Smith')</span>
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$SchemalessUser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1234</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>
$looseSchema プロパティで一部のスキーマを明示的に設定しておくこともできます。下の例ではプライマリキーを key に変更し、更新日時を入力するために updated フィールドを定義しています。
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> LooseSchemaUser <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useDbConfig</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'memcache'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$primaryKey</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'key'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$looseSchema</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                           <span style="color: #0000ff;">'updated'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'datetime'</span><span style="color: #009900;">&#41;</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;">// in Controllers </span>
<span style="color: #000088;">$LooseSchemaUser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'key'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1234</span><span style="color: #339933;">,</span>
                             <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// array('key' =&gt; 1234, 'name' =&gt; 'John Smith', 'updated' =&gt; '2010-02-07 22:23:13')</span>
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$LooseSchemaUser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1234</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>
$looseSchemaを設定せず、$_schema を設定すれば通常の動作になります。
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> StrictUser <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$useDbConfig</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'memcache'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$_schema</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                       <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'string'</span><span style="color: #009900;">&#41;</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;">// in Controllers </span>
<span style="color: #000088;">$StrictUser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">save</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1234</span><span style="color: #339933;">,</span>
                        <span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #339933;">,</span>
                        <span style="color: #0000ff;">'hobby'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'baseball'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'soccer'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// hobby は保存されない</span>
<span style="color: #666666; font-style: italic;">// array('id' =&gt; 1234, 'name' =&gt; 'John Smith')</span>
<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$StrictUser</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">read</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1234</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>
&#8230;
</p>
<p>
裏側では <a href="http://github.com/tkyk/cakephp-key-value-source/blob/master/models/behaviors/key_value_loose_schema.php">KeyValueLooseSchemaBehavior</a> というBehaviorを使って、$_schema に存在しないデータを一旦別のフィールドに退避しておき、DataSource　の中で取り出しています。退避用のフィールドはデフォルトでは _schemaless_data という名前で、DataSource の describe メソッドの中で組み込まれます。さらに（ちょっと反則気味ですが） Behavior 自体も describe メソッドの中で $actsAs に設定することで、特別な設定なしで動くようになっています。
</p>
<p>
自動設定される部分を書き下すと、おおむね次のようになります。
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'KeyValueLooseSchema'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'schemalessField'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'_schemaless_data'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$_schema</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'string'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'length'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                        <span style="color: #0000ff;">'_schemaless_data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'schemaless'</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>



<p>
KeyValueLooseSchemaBehavior の実装は汎用的になっているので、他の DataSource にほとんどそのまま持っていけるはずです。
</p>

]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1112/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CakePHP用Cicindelaライブラリ</title>
		<link>http://wp.serpere.info/archives/1111</link>
		<comments>http://wp.serpere.info/archives/1111#comments</comments>
		<pubDate>Sun, 31 Jan 2010 14:21:43 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[cicindela]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1111</guid>
		<description><![CDATA[
CakePHP から レコメンデーションエンジン Cicindela にアクセスするためのライブラリを公開しました。

http://github.com/tkyk/cakephp-cicindela

使い方はREADMEを読んでください。Cicindela本体のWeb APIとほとんど同じ構造なので、特に迷うことはないだろうと思います。


ちなみに Configure::read(&#8216;debug&#8217;) > 1 で実行すると、DboSourceと同じようにリクエスト情報が一覧表示されます。デバッグに便利です。こんな感じ：

(cicindela) 2 request(s)
NrURLStatusTook (ms)
1http://host/cicindela/recommend?set=pick&#038;op=for_item&#038;item_id=RubyHTTP/1.1 200 OK2887
2http://host/cicindela/recommend?set=pick&#038;op=for_item&#038;item_id=PHPHTTP/1.1 200 OK1346


&#8230;.
以下、このライブラリを作りながら考えたこと。


このライブラリはDataSource + Behaviorという2クラス構成になっている。最初はDataSourceだけでできるかと思っていたのだが、無理だった。現在のCakeのModelクラスは、DataSourceとDboSourceの関係で言うなら&#8221;DboModel&#8221;であって、非DboのDataSourceを扱うには適していない。


現在のModelが&#8221;DboModel&#8221;であるように、XXXSourceに対してはXXXModelが存在することが望ましい。CakePHP 1.3からはDataSourceがプラグイン化できるようなので、同時にModelのベースクラスを提供するようにすれば、ある程度は改善されると思う。ただそれでも結局（プラグインの）ユーザがいちいちクラスの継承関係に気を使わなければいけない点が煩わしい。理想を言うなら、1.非常に簡素なBaseModelクラスを用意する、2.DataSourceにモデル初期化用コールバックを設ける、3.初期化用コールバックにおいてBehaviorをattachする、DataSource固有の機能はBehaviorに実装する……という構成にできれば良いのではないか。

]]></description>
			<content:encoded><![CDATA[
<p>CakePHP から レコメンデーションエンジン Cicindela にアクセスするためのライブラリを公開しました。
</p>
<a href="http://github.com/tkyk/cakephp-cicindela">http://github.com/tkyk/cakephp-cicindela</a>
<p>
使い方はREADMEを読んでください。Cicindela本体の<a href="http://code.google.com/p/cicindela2/wiki/WebAPI">Web API</a>とほとんど同じ構造なので、特に迷うことはないだろうと思います。
</p>
<p>
ちなみに Configure::read(&#8216;debug&#8217;) > 1 で実行すると、DboSourceと同じようにリクエスト情報が一覧表示されます。デバッグに便利です。こんな感じ：
</p>
<table><caption>(cicindela) 2 request(s)</caption>
<tr><td>Nr</td><td>URL</td><td>Status</td><td>Took (ms)</tr></tr>
<tr><td>1</td><td>http://host/cicindela/recommend?set=pick&#038;op=for_item&#038;item_id=Ruby</td><td>HTTP/1.1 200 OK</td><td>2887</tr></tr>
<tr><td>2</td><td>http://host/cicindela/recommend?set=pick&#038;op=for_item&#038;item_id=PHP</td><td>HTTP/1.1 200 OK</td><td>1346</tr></tr>
</table>
<p>
&#8230;.<br />
以下、このライブラリを作りながら考えたこと。
</p>
<p>
このライブラリはDataSource + Behaviorという2クラス構成になっている。最初はDataSourceだけでできるかと思っていたのだが、無理だった。現在のCakeのModelクラスは、DataSourceとDboSourceの関係で言うなら&#8221;DboModel&#8221;であって、非DboのDataSourceを扱うには適していない。
</p>
<p>
現在のModelが&#8221;DboModel&#8221;であるように、XXXSourceに対してはXXXModelが存在することが望ましい。CakePHP 1.3からはDataSourceがプラグイン化できるようなので、同時にModelのベースクラスを提供するようにすれば、ある程度は改善されると思う。ただそれでも結局（プラグインの）ユーザがいちいちクラスの継承関係に気を使わなければいけない点が煩わしい。理想を言うなら、1.非常に簡素なBaseModelクラスを用意する、2.DataSourceにモデル初期化用コールバックを設ける、3.初期化用コールバックにおいてBehaviorをattachする、DataSource固有の機能はBehaviorに実装する……という構成にできれば良いのではないか。
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1111/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>XPCNativeWrapperとunsafeWindowの間で同期処理を行う</title>
		<link>http://wp.serpere.info/archives/1110</link>
		<comments>http://wp.serpere.info/archives/1110#comments</comments>
		<pubDate>Sat, 30 Jan 2010 04:06:00 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Greasemonkey]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1110</guid>
		<description><![CDATA[

先日使い方を学んだ MessageEvent と、location.href+javascript:ハックとJSDeferred userscript版とを組み合わせ、XPCNativeWrapper と unsafeWindow の間で同期処理を行うコードを書いてみた。


まずは MessageEvent を使って前回 evalInPage 相当の処理を作る。キャンセル処理も入れたかったので UnsafeWrapper というオブジェクトにまとめることにした。前回より長くなったように見えるが、実際には MessageEvent の lastEventId が使えるようになった分、簡潔になっている。



var UnsafeWrapper = new &#40;function&#40;&#41; &#123;
	var seqId = 0, waiting = &#123;&#125;, self = this, noop = function&#40;&#41;&#123;&#125;;
&#160;
	function dispatch&#40;data, id&#41; &#123;
	    var e = document.createEvent&#40;&#34;MessageEvent&#34;&#41;;
	    e.initMessageEvent&#40;'GM_UnsafeWrapper_returned', true, false,
			      [...]]]></description>
			<content:encoded><![CDATA[
<p>
<a href="http://wp.serpere.info/archives/1107">先日</a>使い方を学んだ MessageEvent と、location.href+javascript:ハックと<a href="http://github.com/cho45/jsdeferred">JSDeferred</a> userscript版とを組み合わせ、XPCNativeWrapper と unsafeWindow の間で同期処理を行うコードを書いてみた。
</p>
<p>
まずは MessageEvent を使って前回 evalInPage 相当の処理を作る。キャンセル処理も入れたかったので UnsafeWrapper というオブジェクトにまとめることにした。前回より長くなったように見えるが、実際には MessageEvent の lastEventId が使えるようになった分、簡潔になっている。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> UnsafeWrapper <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</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> seqId <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> waiting <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> noop <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;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">function</span> dispatch<span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003366; font-weight: bold;">var</span> e <span style="color: #339933;">=</span> document.<span style="color: #660066;">createEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;MessageEvent&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    e.<span style="color: #660066;">initMessageEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GM_UnsafeWrapper_returned'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
			       data<span style="color: #339933;">,</span> location.<span style="color: #660066;">protocol</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;//&quot;</span> <span style="color: #339933;">+</span> location.<span style="color: #660066;">host</span><span style="color: #339933;">,</span>
			       id<span style="color: #339933;">,</span> window<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    document.<span style="color: #660066;">dispatchEvent</span><span style="color: #009900;">&#40;</span>e<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> listen<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'GM_UnsafeWrapper_returned'</span><span style="color: #339933;">,</span> func<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	listen<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: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>waiting<span style="color: #009900;">&#91;</span>e.<span style="color: #660066;">lastEventId</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		    waiting<span style="color: #009900;">&#91;</span>e.<span style="color: #660066;">lastEventId</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		    <span style="color: #000066; font-weight: bold;">delete</span> waiting<span style="color: #009900;">&#91;</span>e.<span style="color: #660066;">lastEventId</span><span style="color: #009900;">&#93;</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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">exec</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>func<span style="color: #339933;">,</span> args<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #003366; font-weight: bold;">var</span> userFunc <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;(&quot;</span><span style="color: #339933;">+</span> func <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;).apply(null,&quot;</span><span style="color: #339933;">+</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>args <span style="color: #339933;">||</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
	    waiting<span style="color: #009900;">&#91;</span>seqId<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> callback <span style="color: #339933;">||</span> noop<span style="color: #339933;">;</span>
	    location.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;javascript:void &quot;</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>dispatch<span style="color: #339933;">,</span> ret<span style="color: #339933;">,</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> dispatch<span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>ret<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> id<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: #3366CC;">&quot;(&quot;</span><span style="color: #339933;">+</span> dispatch <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #339933;">+</span> userFunc <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>seqId<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
	    <span style="color: #000066; font-weight: bold;">return</span> seqId<span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">cancel</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #000066; font-weight: bold;">delete</span> waiting<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</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: #339933;">;</span></pre></div></div>



<p>
そして Deferred に対するアダプタを書く。とても簡単。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">with</span><span style="color: #009900;">&#40;</span>D<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">function</span> unsafeExec<span style="color: #009900;">&#40;</span>func<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> d <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Deferred<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> UnsafeWrapper.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>func<span style="color: #339933;">,</span> args<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ret<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		d.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>ret<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>
	d.<span style="color: #660066;">canceller</span> <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> UnsafeWrapper.<span style="color: #660066;">cancel</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">return</span> d<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



<p>
これで例えば、GM_xmlhttpRequest で外部ドメインからデータを取得して、そのデータを整形して、unsafeWindow 側で表示処理を行って、そしてその結果をuserscript側で受け取って…という処理は次のように書くことができる。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">with</span><span style="color: #009900;">&#40;</span>D<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
xhttp.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http://example.com/&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</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>
            <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> res.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
            <span style="color: #006600; font-style: italic;">//....</span>
&nbsp;
	    <span style="color: #000066; font-weight: bold;">return</span> unsafeExec<span style="color: #009900;">&#40;</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: #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>k<span style="color: #339933;">,</span>v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                      <span style="color: #006600; font-style: italic;">//...</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;">//...</span>
		    <span style="color: #000066; font-weight: bold;">return</span> result<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#91;</span>data<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: #009900;">&#41;</span>
    .<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #006600; font-style: italic;">//...</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>



<p>
より実用性を高めるのであれば、location.href で実行するコード全体を try {} で囲み、catch したエラーをJSONに変換して _failed イベントを dispatch して、Deferred の fail に転送すれば、unsafeWindow 側で起こったエラーをuserscript側のDeferred chainで捕捉できるようになる。
</p>
<p>参考：</p>
<ul>
<li><a href="http://d.hatena.ne.jp/Constellation/20090326/1238073714">unsafeExec on JSDeferred &#8211; 枕を欹てて聴く</a></li>
<li><a href="http://cho45.stfuawsc.com/jsdeferred/doc/intro.html">JSDeferred 紹介</a></li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1110/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL MEMORYテーブルのサイズをmuninで監視する</title>
		<link>http://wp.serpere.info/archives/1109</link>
		<comments>http://wp.serpere.info/archives/1109#comments</comments>
		<pubDate>Mon, 25 Jan 2010 14:49:47 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[サーバ管理]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1109</guid>
		<description><![CDATA[

DB内のMEMORYテーブルのサイズを、テーブルごとにグラフ化するための munin プラグインを作ってみました。

mysql_memory_tables_
インストール

munin-node 本体のプラグインディレクトリ（/usr/share/munin/plugins など）にコピーしてください。

設定

ワイルドカードプラグインなので、mysql_memory_tables_{チェック対象となるDB名} という名前でシンボリックリンクを張ってください。



ln -s /usr/share/munin/plugins/mysql_memory_tables_ /etc/munin/plugins/mysql_memory_tables_db1




データを取得するには対象DBの INFORMATION_SCHEMA にアクセスする権限が必要です。プラグイン設定ファイル（/etc/munin/plugin-conf.d/munin-node など）で mysql コマンドに渡すユーザ名・パスワードを指定してください。


[mysql_memory_tables_*]
env.mysqlopts -u cicindela --password=hogehoge

動作確認

munin-run コマンドでテストができます。



# 値の表示
munin-run mysql_memory_tables_db1
&#160;
# グラフ設定情報の表示
munin-run mysql_memory_tables_db1 config




動作が確認できたら munin-node を再起動してください。

]]></description>
			<content:encoded><![CDATA[
<p>
DB内のMEMORYテーブルのサイズを、テーブルごとにグラフ化するための munin プラグインを作ってみました。
</p>
<p><a href="http://gist.github.com/285906">mysql_memory_tables_</a></p>
<h3>インストール</h3>
<p>
munin-node 本体のプラグインディレクトリ（/usr/share/munin/plugins など）にコピーしてください。
</p>
<h3>設定</h3>
<p>
ワイルドカードプラグインなので、<code>mysql_memory_tables_{チェック対象となるDB名}</code> という名前でシンボリックリンクを張ってください。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>share<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span>plugins<span style="color: #000000; font-weight: bold;">/</span>mysql_memory_tables_ <span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>munin<span style="color: #000000; font-weight: bold;">/</span>plugins<span style="color: #000000; font-weight: bold;">/</span>mysql_memory_tables_db1</pre></div></div>



<p>
データを取得するには対象DBの INFORMATION_SCHEMA にアクセスする権限が必要です。プラグイン設定ファイル（/etc/munin/plugin-conf.d/munin-node など）で mysql コマンドに渡すユーザ名・パスワードを指定してください。
</p>
<pre>
[mysql_memory_tables_*]
env.mysqlopts -u cicindela --password=hogehoge
</pre>
<h3>動作確認</h3>
<p>
munin-run コマンドでテストができます。
</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># 値の表示</span>
munin-run mysql_memory_tables_db1
&nbsp;
<span style="color: #666666; font-style: italic;"># グラフ設定情報の表示</span>
munin-run mysql_memory_tables_db1 config</pre></div></div>



<p>
動作が確認できたら munin-node を再起動してください。
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1109/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XPCNativeWrapperとunsafeWindowの間でデータを送受信する</title>
		<link>http://wp.serpere.info/archives/1107</link>
		<comments>http://wp.serpere.info/archives/1107#comments</comments>
		<pubDate>Sat, 23 Jan 2010 13:59:30 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Greasemonkey]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1107</guid>
		<description><![CDATA[


XPCNativeWrapperの外側でスクリプトを評価する関数。Firebugも同じことをしている。unsafeWindowを触らないので安全。



function evalInPage&#40;fun&#41; &#123;
  location.href = &#34;javascript:void (&#34; + fun + &#34;)()&#34;;
&#125;



SmartLDR更新 &#8211; 素人がプログラミングを勉強するブログ


これは面白い！Function.prototype.toString が関数のソースコードを返すことを利用した、巧妙にして簡潔なハック。


次のように引数を渡せるように改良すればさらに強力になる（引数に渡せるのはJSON化可能な値のみ）。



function evalInPage&#40;func, args&#41; &#123;
    var argStr = JSON.stringify&#40;args &#124;&#124; &#91;&#93;&#41;;
    location.href = &#34;javascript:void &#34;+ func +&#34;.apply(null,&#34;+ argStr +&#34;)&#34;;
&#125;




GM_xmlhttpRequest を使って別ドメインから得たデータを、unsafeWindow 上のライブラリを使って表示する、といったコードが安全かつ自然に書けるようになる。



// データの取得は Greasemonkey で行う
GM_xmlhttpRequest&#40;&#123; method:&#34;GET&#34;,
            [...]]]></description>
			<content:encoded><![CDATA[
<blockquote>
<p>
XPCNativeWrapperの外側でスクリプトを評価する関数。Firebugも同じことをしている。unsafeWindowを触らないので安全。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> evalInPage<span style="color: #009900;">&#40;</span>fun<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  location.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;javascript:void (&quot;</span> <span style="color: #339933;">+</span> fun <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;)()&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



<p><cite><a href="http://d.hatena.ne.jp/javascripter/20090324/1237903880">SmartLDR更新 &#8211; 素人がプログラミングを勉強するブログ</a></cite></p>
</blockquote>
<p>
これは面白い！Function.prototype.toString が関数のソースコードを返すことを利用した、巧妙にして簡潔なハック。
</p>
<p>
次のように引数を渡せるように改良すればさらに強力になる（引数に渡せるのはJSON化可能な値のみ）。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> evalInPage<span style="color: #009900;">&#40;</span>func<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> argStr <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>args <span style="color: #339933;">||</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    location.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;javascript:void &quot;</span><span style="color: #339933;">+</span> func <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;.apply(null,&quot;</span><span style="color: #339933;">+</span> argStr <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



<p>
GM_xmlhttpRequest を使って別ドメインから得たデータを、unsafeWindow 上のライブラリを使って表示する、といったコードが安全かつ自然に書けるようになる。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// データの取得は Greasemonkey で行う</span>
GM_xmlhttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> method<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span>
                    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;http://example.com/api&quot;</span><span style="color: #339933;">,</span>
                    <span style="color: #000066;">onload</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                      <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> response.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
                      <span style="color: #006600; font-style: italic;">//...</span>
                      evalInPage<span style="color: #009900;">&#40;</span>render<span style="color: #339933;">,</span> <span style="color: #009900;">&#91;</span>data<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: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// データの表示は unsafeWindow 上で行う</span>
<span style="color: #006600; font-style: italic;">// jQueryなど unsafeWindow 上のライブラリが使える</span>
<span style="color: #003366; font-weight: bold;">function</span> render<span style="color: #009900;">&#40;</span>data<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>k<span style="color: #339933;">,</span>v<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//...</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



<p>
これだけでも十分便利なのだが、「返り値が利用できない」「Firefoxのlocation.hrefは非同期で実行される」といった難点があるらしい。それを回避するために<a href="http://d.hatena.ne.jp/Constellation/20090326/1238073714">unsafeExec on JSDeferred &#8211; 枕を欹てて聴く</a>においてはJSDeferredというライブラリを使う方法が紹介されているのだが、コードを見る限り unsafeWindow に対するアクセスが必要になるようだ。
</p>
<p>
unsafeWindow にアクセスせずにGreasemonkeyに制御を戻すには、DOM Eventを使うのが良いと思う。以下、DOMNodeInserted イベントを使って実装してみた（返り値として利用できるのはJSON化可能な値のみ）。
</p>
<p>
<ins datetime="2010-01-24T02:29:03+00:00"><strong>追記:</strong> <a href="http://wp.serpere.info/archives/1107/comment-page-1#comment-809">Constellationさんからコメントをいただきました</a>。DOMNodeInsertedなどのイベントを使わなくても、直接createEvent/dispatchEventでイベントを生成・データを送受信する方法があるようです。 詳細はこのページの Constellationさんのコメントや、『<a href="http://nanto.asablo.jp/blog/2008/06/26/3596261">Greasemonkey スクリプトとイベントで通信: Days on the Moon</a>』など参照してください。</ins>
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> getUniqId<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;">do</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;_tmp&quot;</span> <span style="color: #339933;">+</span> String<span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</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;">while</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> id<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> evalInPage<span style="color: #009900;">&#40;</span>func<span style="color: #339933;">,</span> args<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> argStr <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>args <span style="color: #339933;">||</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        userFunc <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;(&quot;</span><span style="color: #339933;">+</span> func <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;).apply(null,&quot;</span><span style="color: #339933;">+</span> argStr <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        location.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;javascript:void &quot;</span><span style="color: #339933;">+</span> userFunc<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: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        id <span style="color: #339933;">=</span> getUniqId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    div.<span style="color: #660066;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
    div.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span>
    document.<span style="color: #660066;">body</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>div<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    div.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'DOMNodeInserted'</span><span style="color: #339933;">,</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>
            callback<span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">parse</span><span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">target</span>.<span style="color: #660066;">nodeValue</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            div.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span>div<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: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    location.<span style="color: #660066;">href</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;javascript:void &quot;</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>ret<span style="color: #339933;">,</span> id<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>
            .<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>document.<span style="color: #660066;">createTextNode</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>ret<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;.call(null,&quot;</span><span style="color: #339933;">+</span> userFunc <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;)&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



<p>JSDreferredを使う方法と同じく、戻り値を受け取るコールバック関数を指定する。</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">evalInPage<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> num<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//この中のコードは unsafeWindow で実行される</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#123;</span> answer<span style="color: #339933;">:</span> obj.<span style="color: #660066;">a</span> <span style="color: #339933;">+</span> num <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>a<span style="color: #339933;">:</span><span style="color: #CC0000;">99</span><span style="color: #009900;">&#125;</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>ret<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">//この中のコードは Greasemonkey で実行される</span>
	alret<span style="color: #009900;">&#40;</span>ret.<span style="color: #660066;">answer</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>
多分まじめに作り込めば一種のRPCシステムみたいになると思う。
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1107/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>JavaScriptで確実にglobalオブジェクトを得る方法</title>
		<link>http://wp.serpere.info/archives/1106</link>
		<comments>http://wp.serpere.info/archives/1106#comments</comments>
		<pubDate>Fri, 22 Jan 2010 08:59:41 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1106</guid>
		<description><![CDATA[

確実にグローバル関数/変数にアクセスしたいとき。あるいはglobalオブジェクトの名前が分からないときに。



var global = &#40;function&#40;&#41;&#123; return this &#125;&#41;&#40;&#41;;




ブラウザ上では window がglobalオブジェクトを指すことは暗黙の前提になっているが、そうでない環境もあり得る。


ちなみに Greasemonkey と Chrome user-script ではともに window でglobalオブジェクトにアクセスできるようだ。

]]></description>
			<content:encoded><![CDATA[
<p>
確実にグローバル関数/変数にアクセスしたいとき。あるいはglobalオブジェクトの名前が分からないときに。
</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> global <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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</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>
ブラウザ上では <code>window</code> がglobalオブジェクトを指すことは暗黙の前提になっているが、そうでない環境もあり得る。
</p>
<p>
ちなみに Greasemonkey と Chrome user-script ではともに <code>window</code> でglobalオブジェクトにアクセスできるようだ。
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1106/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Behavior の適用範囲を広げる $mapMethods</title>
		<link>http://wp.serpere.info/archives/1105</link>
		<comments>http://wp.serpere.info/archives/1105#comments</comments>
		<pubDate>Thu, 21 Jan 2010 09:14:29 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[CakePHP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1105</guid>
		<description><![CDATA[

(CakePHP 1.2.5)
ビヘイビアに定義されたメソッドは、モデル自身のメソッドであるかのように呼び出すことができます。



class FooBehavior extends ModelBehavior &#123;
  function foo&#40;$model&#41;&#123; /* ... */ &#125;
&#125;
&#160;
class Bar extends AppModel &#123;
  var $actsAs = array&#40;'Foo'&#41;;
&#125;
&#160;
// in Controllers
$this-&#62;Bar-&#62;foo&#40;&#41;;




しかしこの方法では Dynamic Finder のように動的に名前が変化するメソッドは実装できません。


ModelBehavior クラスの $mapMethods プロパティを使用すると、この制約を乗り越えることができます。$mapMethods は配列で、次のような構造をしています：



array&#40; '/メソッド名に対する正規表現/' =&#62; '実際に呼び出されるメソッド'  &#41;




例えば '/^list/' => '_genericList' という設定を行った場合、listから始まる全てのメソッド呼び出しが、ビヘイビアの _genericList メソッドの呼び出しに変換されます。このとき _genericList メソッドの引数には元のメソッド名と引数が渡されます。ちょうど __call によるメソッドのオーバーロードと同じです。


この機能を活用して、あるカラムの値を配列として取得するための listColumn メソッドを作ってみます（Column はカラム名）。このメソッドは ListingBehavior に実装することにしましょう。



class ListingBehavior extends ModelBehavior &#123;
 [...]]]></description>
			<content:encoded><![CDATA[
<p>
(CakePHP 1.2.5)<br />
ビヘイビアに定義されたメソッドは、モデル自身のメソッドであるかのように呼び出すことができます。
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FooBehavior <span style="color: #000000; font-weight: bold;">extends</span> ModelBehavior <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Foo'</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;">// in Controllers</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Bar</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">foo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>
しかしこの方法では Dynamic Finder のように動的に名前が変化するメソッドは実装できません。
</p>
<p>
ModelBehavior クラスの $mapMethods プロパティを使用すると、この制約を乗り越えることができます。$mapMethods は配列で、次のような構造をしています：
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'/メソッド名に対する正規表現/'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'実際に呼び出されるメソッド'</span>  <span style="color: #009900;">&#41;</span></pre></div></div>



<p>
例えば <code>'/^list/' => '_genericList'</code> という設定を行った場合、listから始まる全てのメソッド呼び出しが、ビヘイビアの _genericList メソッドの呼び出しに変換されます。このとき _genericList メソッドの引数には元のメソッド名と引数が渡されます。ちょうど __call によるメソッドのオーバーロードと同じです。
</p>
<p>
この機能を活用して、あるカラムの値を配列として取得するための listColumn メソッドを作ってみます（Column はカラム名）。このメソッドは ListingBehavior に実装することにしましょう。
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ListingBehavior <span style="color: #000000; font-weight: bold;">extends</span> ModelBehavior <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$mapMethods</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^list[a-zA-z0-9]+$/'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'_listColumn'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">function</span> _listColumn<span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">,</span> <span style="color: #000088;">$methodName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</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: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$column</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$methodName</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> am<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fields'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">alias</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$model</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</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: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</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> Set<span style="color: #339933;">::</span><span style="color: #990000;">extract</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/<span style="color: #006699; font-weight: bold;">{$model-&gt;alias}</span>/<span style="color: #006699; font-weight: bold;">{$column}</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$arr</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>



<p>
モデルにこのビヘイビアを組み込むと、任意のカラムに対して listXXX メソッドを呼び出すことができるようになります。
</p>


<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User <span style="color: #000000; font-weight: bold;">extends</span> AppModel <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$actsAs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Listing'</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;">// in Controllers</span>
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">listId</span><span style="color: #009900;">&#40;</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;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">listUsername</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'conditions'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'active = 1'</span><span style="color: #009900;">&#41;</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;">User</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">listGroup_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>



<p>
メソッド名は小文字に変換されて渡されるので（これが仕様なのかバグなのか微妙なところです）、残念ながら Inflector::underscore を使うことはできません。
</p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1105/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ow.ly や oneclip.jp のフレームを外す user script</title>
		<link>http://wp.serpere.info/archives/1104</link>
		<comments>http://wp.serpere.info/archives/1104#comments</comments>
		<pubDate>Tue, 19 Jan 2010 13:52:31 +0000</pubDate>
		<dc:creator>tkyk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Greasemonkey]]></category>

		<guid isPermaLink="false">http://wp.serpere.info/?p=1104</guid>
		<description><![CDATA[

ow.lyやoneclip.jpなど、元コンテンツをiframe内に表示するタイプのURL短縮サービスから、自動的に元URLにリダイレクトするuser scriptを作りました。


Redirect to the iframed URL


同様の機能を持つものとしてiframe redirectorがあり、今まで私も使用していたのですが、対象サービスのURLやHTML構造に依存するコードが含まれているため、対象サービスの構成が変化すると動かなくなってしまいます（少し前に am6.jp の仕様が変わって動かなくなりました。現在は修正されています）。


そのため拙作のスクリプトではURLやHTMLに依存するコードは使わず、「面積比でbodyの4分の1を占めるiframe」を元コンテンツと判断するようにしました。おそらくほとんどのケースで問題なく動作するものと思われます。


デフォルトでは以下のURLで動作します。


http://ow.ly/*
http://oneclip.jp/*
http://am6.jp/*


今後新たに同種のサイトが登場した場合は（登場してほしくないのですが）、Greasemonkeyの管理ダイアログから「ユーザスクリプトを実行するページ」にURLを追加してください。スクリプトの修正は不要です。


Firefox 3.5.7 + Greasemonkey 0.8.2 および Mac版 Google Chrome 4.0.295.9 で動作確認しています。

参考
URL短縮サービス ow.ly は実URLが表示されずに危険 → グリモン書いた &#8211; まちゅダイアリー(2009-11-05)
]]></description>
			<content:encoded><![CDATA[
<p>
<a href="http://ow.ly/">ow.ly</a>や<a href="http://oneclip.jp/">oneclip.jp</a>など、元コンテンツをiframe内に表示するタイプのURL短縮サービスから、自動的に元URLにリダイレクトするuser scriptを作りました。
</p>
<p>
<a href="http://userscripts.org/scripts/show/66765">Redirect to the iframed URL</a>
</p>
<p>
同様の機能を持つものとして<a href="http://userscripts.org/scripts/show/59863">iframe redirector</a>があり、今まで私も使用していたのですが、対象サービスのURLやHTML構造に依存するコードが含まれているため、対象サービスの構成が変化すると動かなくなってしまいます（少し前に am6.jp の仕様が変わって動かなくなりました。現在は修正されています）。
</p>
<p>
そのため拙作のスクリプトではURLやHTMLに依存するコードは使わず、「面積比でbodyの4分の1を占めるiframe」を元コンテンツと判断するようにしました。おそらくほとんどのケースで問題なく動作するものと思われます。
</p>
<p>
デフォルトでは以下のURLで動作します。
</p>
<ul>
<li>http://ow.ly/*</li>
<li>http://oneclip.jp/*</li>
<li>http://am6.jp/*</li>
</ul>
<p>
今後新たに同種のサイトが登場した場合は（登場してほしくないのですが）、Greasemonkeyの管理ダイアログから「ユーザスクリプトを実行するページ」にURLを追加してください。スクリプトの修正は不要です。
</p>
<p>
Firefox 3.5.7 + Greasemonkey 0.8.2 および Mac版 Google Chrome 4.0.295.9 で動作確認しています。
</p>
<h3>参考</h3>
<p><a href="http://www.machu.jp/diary/20091105.html#p01" target="_blank">URL短縮サービス <span class="highlight">ow.ly</span> は実URLが表示されずに危険 → グリモン書いた &#8211; まちゅダイアリー(2009-11-05)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://wp.serpere.info/archives/1104/feed</wfw:commentRss>
		<slash:comments>1</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+= [...]]]></description>
			<content:encoded><![CDATA[
<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;
     [...]]]></description>
			<content:encoded><![CDATA[
<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>
	</channel>
</rss>

<!-- Dynamic page generated in 2.190 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-02-09 14:04:35 -->
