Skip to content

Commit 0e6d464

Browse files
docs(mvc.Collection): add docs and testing for updated collection met… (#2474)
1 parent bffa2c0 commit 0e6d464

File tree

12 files changed

+163
-20
lines changed

12 files changed

+163
-20
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<pre class="docs-method-signature"><code>collection.each(fn, [context])</code></pre>
2+
3+
<p>
4+
Iterate over models of a collection, and invoke <code>fn</code> for each model. <code>fn</code> is invoked with 3 arguments:
5+
<i>(model, index, array)</i>.
6+
</p>
7+
8+
<pre><code>const a = new mvc.Model({ id: 1, label: 'a' });
9+
const b = new mvc.Model({ id: 2, label: 'b' });
10+
const c = new mvc.Model({ id: 3, label: 'c' });
11+
const col = new mvc.Collection([a, b, c]);
12+
13+
col.each((model, i) => model.set({ customData: i }));
14+
15+
col.each((model) => console.log(model.get('customData')));
16+
// 0
17+
// 1
18+
// 2
19+
</code></pre>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<pre class="docs-method-signature"><code>collection.filter(fn, [context])</code></pre>
2+
3+
<p>
4+
Iterate over models of a collection, returning an array of all models <code>fn</code> returns truthy for. <code>fn</code> is invoked with three
5+
arguments: <i>(model, index, array)</i>.
6+
</p>
7+
8+
<pre><code>const a = new mvc.Model({ id: 3, label: 'a' });
9+
const b = new mvc.Model({ id: 2, label: 'b' });
10+
const c = new mvc.Model({ id: 1, label: 'c' });
11+
const d = new mvc.Model({ id: 0, label: 'd' });
12+
const col = new mvc.Collection([a, b, c, d]);
13+
14+
console.log(col.filter((model) => model.get('id') === 0).length); // 1
15+
</code></pre>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<pre class="docs-method-signature"><code>collection.first()</code></pre>
2+
3+
<p>
4+
Return the first model of a collection.
5+
</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<pre class="docs-method-signature"><code>collection.includes(value)</code></pre>
2+
3+
<p>
4+
Return <code>true</code> if model is found in a collection.
5+
</p>
6+
7+
<pre><code>const a = new mvc.Model({ id: 3, label: 'a' });
8+
const b = new mvc.Model({ id: 2, label: 'b' });
9+
const c = new mvc.Model({ id: 1, label: 'c' });
10+
const d = new mvc.Model({ id: 0, label: 'd' });
11+
const col = new mvc.Collection([a, b, c]);
12+
13+
console.log(col.includes(a)); // true
14+
console.log(col.includes(d)); // false
15+
</code></pre>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<pre class="docs-method-signature"><code>collection.isEmpty()</code></pre>
2+
3+
<p>
4+
Return <code>true</code> if a collection is empty.
5+
</p>
6+
7+
<pre><code>const col = new mvc.Collection([]);
8+
9+
console.log(col.isEmpty()); // true
10+
11+
col.set([new mvc.Model({ id: 1, label: 'a' })]);
12+
13+
console.log(col.isEmpty()); // false
14+
</code></pre>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<pre class="docs-method-signature"><code>collection.last()</code></pre>
2+
3+
<p>
4+
Return the last model of a collection.
5+
</p>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<pre class="docs-method-signature"><code>collection.map(fn, [context])</code></pre>
2+
3+
<p>
4+
Create an array of values by running each model in the collection through <code>fn</code>. <code>fn</code> is invoked with three arguments:
5+
<i>(model, index, array)</i>.
6+
</p>
7+
8+
<pre><code>const a = new mvc.Model({ id: 3, label: 'a' });
9+
const b = new mvc.Model({ id: 2, label: 'b' });
10+
const c = new mvc.Model({ id: 1, label: 'c' });
11+
const d = new mvc.Model({ id: 0, label: 'd' });
12+
const col = new mvc.Collection([a, b, c, d]);
13+
14+
console.log(col.map((model) => model.get('label')).join(' ')); // 'a b c d'
15+
</code></pre>

packages/joint-core/docs/src/joint/api/mvc/Collection/prototype/pluck.html

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<pre class="docs-method-signature"><code>collection.reduce(fn, [initialValue])</code></pre>
2+
3+
<p>
4+
Reduce a collection to a value which is the accumulated result of running each model in the collection through <code>fn</code>, where each
5+
successive invocation is supplied the return value of the previous. If <code>initialValue</code> is not given, the first model in the collection
6+
is used as the initial value. <code>fn</code> is invoked with four arguments: <i>(accumulator, currentValue, currentIndex, array)</i>.
7+
</p>
8+
9+
<pre><code>const collection = new mvc.Collection([new mvc.Model({ id: 1, label: 'a' })]);
10+
11+
console.log(collection.reduce((acc, model) => acc.get('id') + model.id )); // 2
12+
</code></pre>

packages/joint-core/src/mvc/Collection.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,8 @@ assign(Collection.prototype, Events, {
354354
},
355355

356356
// Runs "reducer" fn over all elements in the collection, in ascending-index order, and accumulates them into a single value
357-
reduce: function(fn, initAcc, context) {
358-
return this.models.reduce(fn, initAcc, context);
357+
reduce: function(fn, initAcc = this.first()) {
358+
return this.models.reduce(fn, initAcc);
359359
},
360360

361361
// Private method to reset all internal state. Called when the collection

0 commit comments

Comments
 (0)