1.0.9 • Published 11 years ago
ampersand-json-patch v1.0.9
ampersand-json-patch
Adds JSON Patch (RFC 6902) support to Ampersand models.
Usage
npm install --save ampersand-json-patch
ampersand-json-patch is used as a mixin.
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.
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:
[ { 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 for all supported operations.