You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<ahref="https://github.com/Volicon/React-MVx/blob/develop/docs/05_Collection/00_Overview.md" target="_blank">Edit on GitHub</a>
83
81
</span>
@@ -111,22 +109,22 @@ <h1>Overview</h1>
111
109
}
112
110
}
113
111
</code></pre>
114
-
<h2id="page_Declarations">Declarations</h2>
115
-
<h4id="page_model+%3D+RecordConstructor"><code>static</code> model = RecordConstructor</h4>
112
+
<h1id="page_Declarations">Declarations</h1>
113
+
<h3id="page_model+%3D+RecordConstructor"><code>static</code> model = RecordConstructor</h3>
116
114
<pre><codeclass="language-javascript">@define
117
115
class Library extends Record.Collection {
118
116
static model = Book;
119
117
}
120
118
</code></pre>
121
119
<p>If defined, you can pass raw attributes objects (and arrays) to add, create, and reset, and the attributes will be converted into a model of the proper type.</p>
122
120
<p>This property is being set automatically for collection types referenced as <code>MyRecord.Collection</code>. In the majority of cases you don't need to define it explicitly.</p>
<p>When creating a Collection, you may choose to pass in the initial array of models. The collection's comparator may be included as an option. Passing false as the comparator option will prevent sorting. If you define an initialize function, it will be invoked when the collection is created. There are a couple of options that, if provided, are attached to the collection directly: model and comparator.
126
124
Pass null for models to create an empty Collection with options.</p>
127
125
<pre><codeclass="language-javascript">var tabs = new TabSet([tab1, tab2, tab3]);
<p>Add a record (or an array of records) to the collection, firing an "add" event for each record, and an "update" event afterwards. If a record property is defined, you may also pass raw attributes objects, and have them be vivified as instances of the record. Returns the added (or preexisting, if duplicate) records. Pass {at: index} to splice the record into the collection at the specified index. If you're adding records to the collection that are already in the collection, they'll be ignored, unless you pass {merge: true}, in which case their attributes will be merged into the corresponding records, firing any appropriate "change" events.</p>
<p>Remove a record (or an array of records) from the collection, and return them. Each record can be a record instance, an id string or a JS object, any value acceptable as the id argument of collection.get. Fires a "remove" event for each record, and a single "update" event afterwards, unless {silent: true} is passed. The record's index before removal is available to listeners as options.index.</p>
<p>Adding and removing records one at a time is all well and good, but sometimes you have so many records to change that you'd rather just update the collection in bulk. Use reset to replace a collection with a new list of records (or attribute hashes), triggering a single "reset" event on completion, and without triggering any add or remove events on any records. Returns the newly-set records.</p>
148
146
<p>Pass null for records to empty your Collection with options.</p>
149
147
<p>Here's an example using reset to bootstrap a collection during initial page load, in a Rails application:</p>
@@ -153,7 +151,7 @@ <h4 id="page_collection.reset%28+records%2C+options%3F+%29">collection.reset( re
153
151
</script>
154
152
</code></pre>
155
153
<p>Calling collection.reset() without passing any records as arguments will empty the entire collection.</p>
<p>The set method performs a "smart" update of the collection with the passed list of records. If a record in the list isn't yet in the collection it will be added; if the record is already in the collection its attributes will be merged; and if the collection contains any records that aren't present in the list, they'll be removed. All of the appropriate "add", "remove", and "change" events are fired as this happens. Returns the touched records in the collection. If you'd like to customize the behavior, you can disable it with options: {add: false}, {remove: false}, or {merge: false}.</p>
158
156
<pre><codeclass="language-javascript">const vanHalen = new Man.Collection([ eddie, alex, stone, roth ]);
159
157
@@ -163,15 +161,15 @@ <h4 id="page_collection.set%28+records%2C+options%3F+%29">collection.set( record
163
161
// Updates any of stone, alex, and eddie's attributes that may have
164
162
// changed over the years.
165
163
</code></pre>
166
-
<h4id="page_collection.get%28+id+%29">collection.get( id )</h4>
164
+
<h3id="page_collection.get%28+id+%29">collection.get( id )</h3>
167
165
<p>Get a record from a collection, specified by an <code>id</code>, a <code>cid</code>, or by passing in a record.</p>
168
166
<pre><codeclass="language-javascript">const book = library.get(110);
169
167
</code></pre>
170
-
<h4id="page_collection.at%28+index+%29">collection.at( index )</h4>
168
+
<h3id="page_collection.at%28+index+%29">collection.at( index )</h3>
171
169
<p>Get a record from a collection, specified by index. Useful if your collection is sorted, and if your collection isn't sorted, at will still retrieve records in insertion order. When passed a negative index, it will retrieve the record from the back of the collection.</p>
<p>Raw access to the JavaScript array of models inside of the collection. Usually you'll want to use <code>get</code>, <code>at</code>, or the other methods to access model objects, but occasionally a direct reference to the array is desired.</p>
<p>Called when <code>{ parse : true }</code> option is used in collection's <code>constructor</code>, <code>set</code>, <code>reset</code>, and <code>add</code> methods.</p>
91
89
<p>May be overriden to transforms the responce from the server.</p>
<p>Return an array containing the attributes hash of each model (via toJSON) in the collection. This can be used to serialize and persist the collection as a whole. The name of this method is a bit confusing, because it conforms to JavaScript's JSON API.</p>
94
92
<pre><codeclass="language-javascript">@define class Man extends Record {
<ahref="https://github.com/Volicon/React-MVx/blob/develop/docs/05_Collection/03_Sorted_collections.md" target="_blank">Edit on GitHub</a>
83
81
</span>
@@ -88,11 +86,11 @@ <h1>Sorted collections</h1>
88
86
<p>By default there is no comparator for a collection. If you define a comparator, it will be used to maintain the collection in sorted order. This means that as records are added, they are inserted at the correct index in collection.models.</p>
89
87
<p>Note that Type-R depends on the arity of your comparator function to determine between the two styles, so be careful if your comparator function is bound.</p>
90
88
<p>Collections with a comparator will not automatically re-sort if you later change model attributes, so you may wish to call sort after changing model attributes that would affect the order.</p>
<p>Maintain the collection in sorted order by the given record's attribute.</p>
93
-
<h4id="page_comparator+%3D+x+%3D%3E+number+%7C+string"><code>static</code> comparator = x => number | string</h4>
91
+
<h3id="page_comparator+%3D+x+%3D%3E+number+%7C+string"><code>static</code> comparator = x => number | string</h3>
94
92
<p>"sortBy" comparator functions take a model and return a numeric or string value by which the model should be ordered relative to others.</p>
<p>"sort" comparator functions take two models, and return -1 if the first model should come before the second, 0 if they are of the same rank and 1 if the first model should come after.</p>
97
95
<p>Note how even though all of the chapters in this example are added backwards, they come out in the proper order:</p>
<p>Force a collection to re-sort itself. You don't need to call this under normal circumstances, as a collection with a comparator will sort itself whenever a model is added. To disable sorting when adding a model, pass <code>{sort: false}</code> to add. Calling sort triggers a "sort" event on the collection.</p>
0 commit comments