PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/docs/Collection.md

https://github.com/junk16/Backbone
Markdown | 127 lines | 93 code | 34 blank | 0 comment | 0 complexity | 38e35c680f90a7955009fe4cba8b2ea3 MD5 | raw file
  1. + 元文書: [backbone/index.html at c36df02d950ea626d036033fedd13c631118cf59 · documentcloud/backbone · GitHub](https://github.com/documentcloud/backbone/blob/c36df02d950ea626d036033fedd13c631118cf59/index.html "backbone/index.html at c36df02d950ea626d036033fedd13c631118cf59 · documentcloud/backbone · GitHub")
  2. ## Backbone.Collection [原文](http://backbonejs.org/#Collection)
  3. Collectionはモデルを集合させたものですコレクション内のモデルが変更された際に通知する`"change"`イベントをバインドする事もできますし`"add"``"remove"`イベントで待ち受ける事もできサーバーからコレクションを`"fetch"`でき[Underscore.jsメソッド](#Collection-Underscore-Methods)の全てのセットを使う事ができます
  4. コレクション内のモデル内でトリガーされた全てのイベントは利便性の為にコレクションにも直接トリガーされますこれによりコレクション内の全てのモデルの特定の属性の変更を待ち受ける事が可能です `Documents.on("change:selected", ...)`
  5. ### extend `Backbone.Collection.extend(properties, [classProperties])` [原文](http://backbonejs.org/#Collection-extend)
  6. 自分の **Collection** クラスを作るもので **Backbone.Collection** の拡張であり **properties** インスタンスやオプションでコレクションのコンストラクタ関数に対して直接付けられる **classProperties** を提供します
  7. ### model `collection.model` [原文](http://backbonejs.org/#Collection-model)
  8. コレクションに含まれる特定のモデルクラスのプロパティを上書きします定義された場合は[add](#Collection-add)[create](#Collection-create)[reset](#Collection-reset)に生の属性オブジェクト(配列)を渡す事ができ属性はモデル内の適切な型に変換されます
  9. ```javascript
  10. var Library = Backbone.Collection.extend({
  11. model: Book
  12. });
  13. ```
  14. ### constructor / initialize `new Collection([models], [options])` [原文](http://backbonejs.org/#Collection-constructor)
  15. Collectionを作成した時に **models** の初期配列に渡すものを選択できますコレクションの[comparator](#Collection-comparator)関数はオプションを含むことができます **initialize** 関数を定義した場合コレクションが作成された時に呼び出されます
  16. ```javascript
  17. var tabs = new TabSet([tab1, tab2, tab3]);
  18. ```
  19. ### models `collection.models` [原文](http://backbonejs.org/#Collection-models)
  20. コレクションの内部のモデルのJavaScriptの配列に生のアクセスをします通常は`get``at`やモデルオブジェクトにアクセスする **Underscoreのメソッド** を使いますが稀に配列に直接参照したい場合もあるでしょう
  21. ### toJSON `collection.toJSON()` [原文](http://backbonejs.org/#Collection-toJSON)
  22. コレクション内のモデルそれぞれの属性のハッシュを含む配列を返しますこれはコレクション全体をシリアライズと永続化する事ができますこのメソッドの名前は[JavaScriptのJSON API](https://developer.mozilla.org/en/JSON#toJSON\(\)_method)と一緒なので、若干区別が付きにくいです。
  23. ```javascript
  24. var collection = new Backbone.Collection([
  25. {name: "Tim", age: 5},
  26. {name: "Ida", age: 26},
  27. {name: "Rob", age: 55}
  28. ]);
  29. alert(JSON.stringify(collection));
  30. ```
  31. ### Underscore Methods (28) [原文](http://backbonejs.org/#Collection-Underscore-Methods)
  32. Backboneは28種のイテレーション関数を **Backbone.Collection** に提供する為に **Underscore.js** を代理していますその全てがこちらにドキュメント化されてはいないですが詳しくはUnderscoreのドキュメントで調べる事ができます…
  33. - [forEach (each)](http://documentcloud.github.com/underscore/#each)
  34. - [map](http://documentcloud.github.com/underscore/#map)
  35. - [reduce (foldl, inject)](http://documentcloud.github.com/underscore/#reduce)
  36. - [reduceRight (foldr)](http://documentcloud.github.com/underscore/#reduceRight)
  37. - [find (detect)](http://documentcloud.github.com/underscore/#find)
  38. - [filter (select)](http://documentcloud.github.com/underscore/#filter)
  39. - [reject](http://documentcloud.github.com/underscore/#reject)
  40. - [every (all)](http://documentcloud.github.com/underscore/#all)
  41. - [some (any)](http://documentcloud.github.com/underscore/#any)
  42. - [include](http://documentcloud.github.com/underscore/#include)
  43. - [invoke](http://documentcloud.github.com/underscore/#invoke)
  44. - [max](http://documentcloud.github.com/underscore/#max)
  45. - [min](http://documentcloud.github.com/underscore/#min)
  46. - [sortBy](http://documentcloud.github.com/underscore/#sortBy)
  47. - [groupBy](http://documentcloud.github.com/underscore/#groupBy)
  48. - [sortedIndex](http://documentcloud.github.com/underscore/#sortedIndex)
  49. - [shuffle](http://documentcloud.github.com/underscore/#shuffle)
  50. - [toArray](http://documentcloud.github.com/underscore/#toArray)
  51. - [size](http://documentcloud.github.com/underscore/#size)
  52. - [first](http://documentcloud.github.com/underscore/#first)
  53. - [initial](http://documentcloud.github.com/underscore/#initial)
  54. - [rest](http://documentcloud.github.com/underscore/#rest)
  55. - [last](http://documentcloud.github.com/underscore/#last)
  56. - [without](http://documentcloud.github.com/underscore/#without)
  57. - [indexOf](http://documentcloud.github.com/underscore/#indexOf)
  58. - [lastIndexOf](http://documentcloud.github.com/underscore/#lastIndexOf)
  59. - [isEmpty](http://documentcloud.github.com/underscore/#isEmpty)
  60. - [chain](http://documentcloud.github.com/underscore/#chain)
  61. ```javascript
  62. Books.each(function(book) {
  63. book.publish();
  64. });
  65. var titles = Books.map(function(book) {
  66. return book.get("title");
  67. });
  68. var publishedBooks = Books.filter(function(book) {
  69. return book.get("published") === true;
  70. });
  71. var alphabetical = Books.sortBy(function(book) {
  72. return book.author.get("name").toLowerCase();
  73. });
  74. ```
  75. ### add `collection.add(models, [options])` [原文](http://backbonejs.org/#Collection-add)
  76. コレクションにモデル(または複数のモデルの配列)を追加します`{silent: true}`を引数で渡すと抑制ができる`"add"`イベントを発火させます[model](#Collection-model)プロパティが指定されている場合は生の属性オブジェクトを渡す事ができモデルのインスタンスとして活発にさせます`{at: index}`を渡すとモデルを特定の`index`のコレクション中に繋ぎ込みます同様にコレクションの`add`イベントのコールバックを待ち受ける場合に`options.index`によってコレクションに追加されたモデルがどのインデックスなのかを知る事ができます
  77. ```javascript
  78. var ships = new Backbone.Collection;
  79. ships.on("add", function(ship) {
  80. alert("Ahoy " + ship.get("name") + "!");
  81. });
  82. ships.add([
  83. {name: "Flying Dutchman"},
  84. {name: "Black Pearl"}
  85. ]);
  86. ```
  87. ### remove `collection.remove(models, [options])` [原文](http://backbonejs.org/#Collection-remove)
  88. コレクションからモデル(または複数のモデルの配列)を削除します`silent`を使う事で抑制ができる`"remove"`イベントを発火させます`"remove"`イベントのコールバックを待ち受ける場合に`options.index`によりコレクションから削除されたモデルがどのインデックスなのかを知る事ができます
  89. ### get `collection.get(id)` [原文](http://backbonejs.org/#Collection-get)
  90. **id** により特定されたモデルをコレクションから取得します
  91. ```javascript
  92. var book = Library.get(110);
  93. ```