Skip to content

Commit

Permalink
2.2.1 - Add reset method
Browse files Browse the repository at this point in the history
  • Loading branch information
masylum committed Jul 13, 2017
1 parent 04d2225 commit e7b4622
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## `2.2.1`

:tophat:

- Added `reset` for conveniently resetting a whole collection

## `2.2.0`

:nail_care:
Expand Down
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,21 @@ const pau = usersCollection.find({ name: 'pau' })
pau.get('name') // => 'pau'
```

#### `add(data: Object): Array<Model>`
#### `add(data: Array<Object>): Array<Model>`

Add a model with the given attributes.
Adds models with the given array of attributes.

```js
usersCollection.add([{id: 1, name: 'foo'}])
```

#### `reset(data: Array<Object>): Array<Model>`

Resets the collection with the given models.

```js
usersCollection.reset([{id: 1, name: 'foo'}])
```

#### `remove(ids: Array<number>): void`

Expand Down
10 changes: 10 additions & 0 deletions __tests__/Collection.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ describe('Collection', () => {
})
})

describe('reset', () => {
it('reset a collection of models', () => {
const newItem = { id: 2, name: 'bob' }
collection.reset([newItem])

expect(collection.models.length).toBe(1)
expect(collection.get(2).get('name')).toBe(newItem.name)
})
})

describe('remove', () => {
it('removes a collection of models', () => {
collection.remove([1])
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-rest",
"version": "2.2.0",
"version": "2.2.1",
"description": "REST conventions for mobx.",
"repository": {
"type": "git",
Expand Down
11 changes: 11 additions & 0 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ export default class Collection<T: Model> {
return models
}

/**
* Resets a collection of models.
* Returns the added models.
*/
@action
reset (data: Array<{ [key: string]: any }>): Array<T> {
const models = data.map(d => this.build(d))
this.models = models
return models
}

/**
* Removes the model with the given ids or uuids
*/
Expand Down

0 comments on commit e7b4622

Please sign in to comment.