diff --git a/package.json b/package.json index 0656a7a..679bdf4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "reste", - "version": "1.6.0", + "version": "1.7.0", "description": "A JavaScript REST / API helper for Titanium with Alloy Models/Collections support", "titaniumManifest": { "guid": "afafe8b0-b93b-771c-a9e5-4e71db81b9ff" diff --git a/readme.md b/readme.md index da5d309..868de60 100644 --- a/readme.md +++ b/readme.md @@ -141,11 +141,11 @@ api.config({ A couple of useful hooks or events can be used within your RESTe global configuration. Those hooks will happen before specific calls are made. They will be executed before any request is sent allowing you to a) change the parameters or b) stop the call happening. -beforePost: +#### beforePost: This one is quite useful if you need to change the parameters which are going to be used for the request. You might for example -- if you're using Parse Server -- want to strip out certain parameters from models before sending them. -Example: +**Example:** ```javascript { ... @@ -156,11 +156,12 @@ Example: ... } ``` -beforeSend: -These are similar to beforeSend but works for all requests (GET, PUT, DELETE, POST). If you specify both beforePost and beforeLoad then beforePost will go first, then beforeSend. +#### beforeSend: -Example: +These are similar to beforeSend but works for all requests (GET, PUT, DELETE, POST). If you specify both `beforePost` and `beforeLoad` then `beforePost` will go first, then `beforeSend`. Inside `beforeSend` or `beforePost` you can call the `callback()` method with `skip:true` so it will still fire your `success` callback but _without_ making the API call. Use this e.g. to load offline fallback data inside your `success` method: + +**Example:** ```javascript { ... @@ -169,6 +170,11 @@ Example: callback(data); } else { alert("No internet connection!"); + callback({ + skip: true, + error: "no_internet" + }); + // will call your success method and pass `error:no_internet` to it } }, ... diff --git a/reste.js b/reste.js index 687b0b2..97378a9 100644 --- a/reste.js +++ b/reste.js @@ -179,11 +179,19 @@ function main() { if (args.method === 'POST' && typeof beforePost === 'function') { beforePost(args.params, (e) => { + if (e.skip) { + onLoad(e); + return; + } args.params = e; send(); }); } else if (typeof beforeSend === 'function') { beforeSend(args.params, (e) => { + if (e.skip) { + onLoad(e); + return; + } args.params = e; send(); }); @@ -581,4 +589,4 @@ function main() { return reste; } -module.exports = main; \ No newline at end of file +module.exports = main;