Skip to content

Commit

Permalink
Merge pull request #78 from m1ga/noInternet
Browse files Browse the repository at this point in the history
Skip property in beforeLoad callback
  • Loading branch information
jasonkneen authored Apr 8, 2024
2 parents e936265 + 2d587d3 commit e54b51d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
43 changes: 24 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,37 +141,42 @@ 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
{
...
beforePost: function(params, callback) {
params.something = 'else';
callback(params);
params.something = 'else';
callback(params);
},
...
}
```
beforeSend:

#### beforeSend:
This is similar to beforePost but works for all requests (GET, PUT, DELETE, POST). If you specify both beforePost and beforeSend then beforePost will go first, then beforeSend.

Example:
**Example:**
```javascript
{
...
beforeSend: function(data, callback) {
if (Ti.Network.online) {
callback(data);
} else {
alert("No internet connection!");
}
},
...
if (Ti.Network.online) {
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
}
},
...
}
```
### Errors
Expand Down Expand Up @@ -277,13 +282,13 @@ Here's a **PUT** request example, passing an id (you'd need to ensure you have a
```javascript
api.updateVideo({
objectId: "123",
body: {
categoryId: 2,
name: "My Video2"
}
objectId: "123",
body: {
categoryId: 2,
name: "My Video2"
}
}, function(video) {
// do stuff with the video
// do stuff with the video
});
```
Expand Down
10 changes: 9 additions & 1 deletion reste.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -581,4 +589,4 @@ function main() {
return reste;
}

module.exports = main;
module.exports = main;

0 comments on commit e54b51d

Please sign in to comment.