https://github.com/gdibble/ampersand-json-patch
Adds JSON Patch (RFC 6902) support to Ampersand models.
https://github.com/gdibble/ampersand-json-patch
Last synced: 12 months ago
JSON representation
Adds JSON Patch (RFC 6902) support to Ampersand models.
- Host: GitHub
- URL: https://github.com/gdibble/ampersand-json-patch
- Owner: gdibble
- License: mit
- Created: 2015-06-02T21:59:43.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-06-05T18:05:40.000Z (about 11 years ago)
- Last Synced: 2025-02-23T18:14:51.535Z (over 1 year ago)
- Language: JavaScript
- Size: 90.8 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ampersand-json-patch
Adds JSON Patch (RFC 6902) support to Ampersand models.
###**[Don't patch like an idiot.](http://williamdurand.fr/2014/02/14/please-do-not-patch-like-an-idiot/)**
## Usage
`npm install --save ampersand-json-patch`
**ampersand-json-patch** is used as a [mixin](https://ampersandjs.com/learn/base-objects-and-mixins#using-and-re-using-mixins).
```javascript
var AmpersandModel = require('ampersand-model');
var PatchMixin = require('ampersand-patch-json');
var CarModel = AmpersandModel.extend(PatchMixin, {
urlRoot: '/api/cars',
props: {
id: 'number',
make: 'string',
model: 'string',
year: 'number'
},
initialize: function() {
this.initPatcher();
}
});
```
Models now have a `patch()` method.
```javascript
var myCar = new CarModel({id: 1, make: 'Honda', model: 'CR-V', year: 1999});
myCar.set({model: 'Civic'});
myCar.patch();
```
Generates the following HTTP PATCH request to `/api/cars/1`:
```javascript
[ { op: 'replace', path: '/model', value: 'Civic' } ]
```
The `patch` method also accepts `success` and `error` callbacks in the options hash, just like Ampersand's `save`.
Initializing the patcher with `initPatcher({listeners: true})` will fire a patch to the server automatically on every `change` event to your model.
See [JSON-Patch](https://github.com/Starcounter-Jack/JSON-Patch) for all supported operations.