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
// let's take the last model out of the collection
143
-
constlastTodo=todos.pop();
151
+
constlastTodo=todos.extractLast();
144
152
console.log(lastTodo); // `My second task`
153
+
console.log(todos.length); // `1`
145
154
```
146
155
147
156
### Observing Models and Collections
@@ -342,6 +351,27 @@ const Todos = createCollection({
342
351
343
352
Collection instances also come with built-in methods like `map`, `filter`, `reduce` just like `Array`. See more in API Reference.
344
353
354
+
### Immutable collections
355
+
356
+
Collections are immutable by default. If you want to use built-in methods that mutate the collection, then you have to do them by defining custom methods first:
357
+
358
+
```js
359
+
constTodos=createCollection({
360
+
model: Todo,
361
+
362
+
addTodo(todo) {
363
+
// `push` and other mutating methods are only available inside custom methods
364
+
returnthis.push(todo);
365
+
},
366
+
});
367
+
368
+
consttodos=newTodos();
369
+
todos.addTodo(newTodo({ title:'First task' })); // works
370
+
371
+
// this will NOT work
372
+
todos.push(newTodo({ title:'Another task' }));
373
+
```
374
+
345
375
## Embedding
346
376
347
377
Models can embed other Models and Collections, and this can go as many levels deep as the data structure demands.
0 commit comments