# ampersand-collection-rest-mixin

> A mixin for extending ampersand-collection with restful methods.

Latest version **6.0.0** (published 2016-05-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install ampersand-collection-rest-mixin
pnpm add ampersand-collection-rest-mixin
yarn add ampersand-collection-rest-mixin
bun add ampersand-collection-rest-mixin
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.0 |
| Published | 2016-05-25 |
| First published | 2014-04-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Henrik Joreteg |
| Maintainers | fyockm, gar, henrikjoreteg, latentflip, lukekarrys, pgilad |
| Keywords | ampersand, collection, underscore |

## Links

- npm: https://www.npmjs.com/package/ampersand-collection-rest-mixin
- Repository: https://github.com/ampersandjs/ampersand-collection-rest-mixin
- Issues: https://github.com/ampersandjs/ampersand-collection-rest-mixin/issues
- npm.io page: https://npm.io/package/ampersand-collection-rest-mixin

## Dependencies (3)

- [lodash](https://npm.io/package/lodash.md) ^4.11.1
- [ampersand-sync](https://npm.io/package/ampersand-sync.md) ^5.0.0
- [ampersand-version](https://npm.io/package/ampersand-version.md) ^1.0.2

## Alternatives

- [lodash.assign](https://npm.io/package/lodash.assign.md) — 2.3M weekly downloads
- [lodash.chunk](https://npm.io/package/lodash.chunk.md) — 1.8M weekly downloads
- [react-native-ios-utilities](https://npm.io/package/react-native-ios-utilities.md) — 138.5K weekly downloads
- [@technically/lodash](https://npm.io/package/@technically/lodash.md) — 50.9K weekly downloads
- [@fluid-topics/ft-icon](https://npm.io/package/@fluid-topics/ft-icon.md) — 20.6K weekly downloads

## Recent versions

- 6.0.0 (latest) — 2016-05-25
- 5.2.0 — 2016-01-25
- 5.1.1 — 2015-11-25
- 5.0.0 — 2015-08-21
- 4.2.0 — 2015-05-20
- 4.1.1 — 2015-03-31
- 4.1.0 — 2015-03-24
- 4.0.0 — 2015-03-19
- 3.0.1 — 2014-10-31
- 3.0.0 — 2014-08-02
- 2.0.0 — 2014-06-23
- 1.1.0 — 2014-06-23
- 1.0.0 — 2014-04-10
- 0.1.1 — 2014-04-09
- 0.1.0 — 2014-04-08

## README

# ampersand-collection-rest-mixin

<!-- starthide -->
Part of the [Ampersand.js toolkit](http://ampersandjs.com) for building clientside applications.
<!-- endhide -->

A mixin for extending ampersand-collection with restful methods. To make it behave much like Backbone.

## install

```
npm install ampersand-collection-rest-mixin
```

## api

##### create(model, options)

Create a new instance of a model in this collection. Add the model to the collection immediately.

###### options

- `wait` - default: `false`. If set to true, we wait for the server to agree before updating the collection.

##### fetch(options)

Fetch the default set of models for this collection, and set them onto this collection.

###### options

- `reset` [boolean] - default: `false`.  Executes the `reset` method on the collection with server response instead of the set method.
- `set` [boolean] - defaut: `true`.  When left true, executes the `reset` method on the collection with server response.  When set to false, the collection will *not be modified* after the HTTP request is complete.  This enables use of of the collection strictly as an API client.  The `success` and `error` handlers are still triggered. Useful in rare circumstances.
- `success(collection, response, options)` [function] - default: `undefined`.  Called after the XHR has completed successfully and the collection is updated.
- `error(model, response, options)` [function] - default: `undefined`.  Called after the XHR has failed.
- `always(err, response, body)` [function] - default: `undefined`. Called when request finishes no matter what the result.

##### fetchById(id, options, cb)

fetches a model and adds it to collection when fetched

##### getOrFetch(id, options, cb)

get or fetch a model by id

###### options

same options as `fetch()`, with the addition of:

- `all` [boolean] - default: `false`. If `true`, this fetches the entire collection and then returns the requested model.

##### sync()

proxy to [ampersand-sync](AmpersandJS/ampersand-sync)

## example

```javascript
var Collection = require('ampersand-collection');
var restMixin = require('ampersand-collection-rest-mixin');


module.exports = Collection.extend(restMixin, {
    sampleMethod: function () {
        // now we've got restful methods in our collection
        this.sync( ... );
        this.fetch( ... );
        this.create( ... )
    }
});
```

## credits

All credit for this approach in backbone goes to Jeremy Ashkenas and the rest of the Backbone and Underscore authors.

## license

MIT

---
_Source: https://npm.io/package/ampersand-collection-rest-mixin · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
