diff --git a/readme.md b/readme.md index b567d27..6577c98 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ ## FUTURE BREAKING CHANGE -In 1.4.5, a new option to support error objects is available. It's off by default and can be switched on by setting: +In 1.4.5, a new option to support error objects is available. It's off by default and can be switched on by setting: ```JS .errorsAsObjects = true @@ -127,7 +127,46 @@ api.config({ }); ``` -**NOTE:** You can't put the config in the same file as one that is using any bindings to the models/collections define *in* the config. This is because Alloy will attempt to resolve any references for **dataCollection** before the config is ready -- so for best results put the config into alloy.js directly OR as a require to a config file in the app/lib folder. +**NOTE:** You can't put the config in the same file as one that is using any bindings to the models/collections define *in* the config. This is because Alloy will attempt to resolve any references for **dataCollection** before the config is ready -- so for best results put the config into alloy.js directly OR as a require to a config file in the app/lib folder. + +### Hooks: beforePost, beforeGet, beforePut, beforeDelete + +Several hooks or events can be used within your RESTe configuration. Those hooks will happen before specific calls are made. They will be executed before any request is sent. + +beforePost: + +This one is quite useful if you need to change the parameters which are going to be used for the request. + +This hook will happen before beforeLoad if both are specified. + +Example: + +{ + ... + beforePost: function(params, callback) { + params.something = 'else'; + callback(params); + }, + ... +} + +beforeSend: + +These are similar to beforeSend but works for all requests (GET, PUT, DELETE, POST) + +Example: + +{ + ... + beforeSend: function(data, callback) { + if (Ti.Network.online) { + callback(data); + } else { + alert("No internet connection!"); + } + }, + ... +} ### Errors diff --git a/reste.js b/reste.js index c51a703..821d7ef 100644 --- a/reste.js +++ b/reste.js @@ -210,23 +210,9 @@ var main = function() { send(); }); - } else if (args.method == "GET" && typeof config.beforeGet == "function") { + } else if (typeof config.beforeSend == "function") { - config.beforeGet(args.params, function(e) { - args.params = e; - send(); - }); - - } else if (args.method == "PUT" && typeof config.beforePut == "function") { - - config.beforePut(args.params, function(e) { - args.params = e; - send(); - }); - - } else if (args.method == "DELETE" && typeof config.beforeDelete == "function") { - - config.beforeDelete(args.params, function(e) { + config.beforeSend(args.params, function(e) { args.params = e; send(); });