prototype.jsでput/deleteするときに_methodが送られない問題に対処した
祝Ajax解禁!ということでprototype.jsを使ってRestfulなWebアプリを製作中です。id:secondlifeさんのRESTWikiから2年弱経っている昨今ですが、これだけ周回遅れならかえって爽快。というか、AjaxとRESTの組み合わせ、現時点でもかなりの破壊力を秘めていると思ってます。何を壊すか知りませんが。
で、本題。RESTといえばリソースのput/deleteです。prototype.jsならサポートしてるだろうと思ってたら、なんか中途半端でした。methodをpostに置き換えるだけで、_methodに何も入れてくれないんだもの。これが意図した動作なのかな?
とりあえず、下記の修正で対処しました(prototype.js 1.5.1)。勉強中ゆえ、細部には気をつかっていません。
986c986,987 < parameters: '' --- > parameters: '', > postBody: '' 992a994,995 > if (typeof this.options.postBody == 'string') > this.options.postBody = this.options.postBody.toQueryParams(); 1012a1016 > var postBody = Object.clone(this.options.postBody); 1016a1021 > postBody['_method'] = this.method; 1020a1026 > this.postBody = postBody; 1028a1035 > postBody = Hash.toQueryString(postBody); 1043c1050,1051 < this.body = this.method == 'post' ? (this.options.postBody || params) : null; --- > // this.body = this.method == 'post' ? (this.options.postBody || params) : null; > this.body = this.method == 'post' ? (postBody || params) : null;