--- layout: old_post title: Jesterに機能追加 find_all permalink: /tatsuya/show/304-jester-find-all ---

Jester-find_all

Jester 一通り必要な機能は揃ってるけど、一つ言うなら一覧取得が欲しい。Base.find_all()とかでpaginate対応だとさらに嬉しいかも。ということでfind_allを機能追加してみた、本当はfind(:all)みたいにしたいけど面倒なのでいいや、こんな感じで。

Base.prototype.find_all = function() {
  var models = [];
  var docs = this._tree.parseHTTP(this.plural_url(), {})[this._plural][this._singular];
  for(var i=0; i<docs.size(); i++){
    models.push( this.build(this.attributesFromTree(docs[i])) );
  }
  return models;
};

//Base.find_allで配列に入ったオブジェクトが帰ってくる
>>> var books = Book.find_all()
GET http://localhost:3000/books.xml (247ms)prototype.js (line 866)
>>> books[1].title
"優しいRailsの育て方"

むふふ良いねJester、シームレスにActiveRecordと繋がる操作感。あとpaginate欲しいな、追加しよ。

追記;ページング対応した... jester_add_tkmr.js
Book.find_all(2) で http://localhost:3000/books.xml?page=2 へGETアクセス。結果が無効の場合 false を返す