Class baja.comm.Callback
Extends
BaseBajaObj.
Network Callback.
A Callback object has ok and fail methods. It is used to make a comms call in BajaScript. When a comms call is made, additional callbacks may be needed to process the incoming data before calling the original ok or fail callbacks are executed. Therefore, extra help functions have been added to this object to for convenience.
This method should only be used internally by BajaScript. It should never appear
in the public API.
Defined in: comm.js.
| Constructor Attributes | Constructor Name and Description |
|---|---|
| <private> |
| Method Attributes | Method Name and Description |
|---|---|
| <private> |
addFail(newFail)
Add a 'fail' callback.
|
| <private> |
addOk(newOk)
Add an 'ok' callback.
|
| <private> |
addReq(channel, key, body)
Add a request to the callback's batch object.
|
| <private> |
If a Batch object was originally passed in when this object was
created then commit the Batch.
|
| <private> |
commit()
Commit the this object's Batch
This asynchronously makes a network call |
| <private> |
Synchronously commit this object's batch
Please note that baja.comm.Commit#commit should always be used in preference as this will result in a synchronous network call that will block everything else. |
| <private> |
getBatch()
Return this object's batch object
|
| <private> |
Return true if the batch object was originally defined when the batch was created.
|
- Methods borrowed from class BaseBajaObj:
- equals, valueOf
When calling some BajaScript that causes a network call, there will be other callbacks that need to be processed before the original 'user' callbacks are invoked. Therefore, 'addOk' and 'addFail' can be used to chain up extra Callbacks if needed.
// Create a callback object with user's original ok and fail function callbacks...
var cb = new baja.comm.Callback(ok, fail, batch);
// Add an intermediate callback...
cb.addFail(function (ok, fail, err) {
// Process the error messages before calling the original 'fail' callback...
var niceMsg = processError(err);
// Now call 'fail' callback passed in with processed error message...
fail(niceMsg);
});
Please note, that when adding an intermediate calllback, the 'ok' or 'fail' method passed in must be called at some point.
- Parameters:
- {Function} newFail
- the callback Function. This function must accept three arguments including the current ok and fail function as well as any arguments specific to the callback function. By convention, the 'ok' or 'fail' functions passed in must be called after the new callback has finished.
- See:
- baja.comm.Callback#fail
When calling some BajaScript that causes a network call, there will be other callbacks that need to be processed before the original 'user' callbacks are invoked. Therefore, 'addOk' and 'addFail' can be used to chain up extra Callbacks if needed.
// Create a callback object with user's original ok and fail function callbacks...
var cb = new baja.comm.Callback(ok, fail, batch);
// Add an intermediate callback...
cb.addOk(function (ok, fail, resp) {
// Process the response 'resp' object...
var val = processResponse(resp);
// Now call 'ok' callback passed in with processed response...
ok(val);
});
Please note, that when adding an intermediate calllback, the 'ok' or 'fail' method passed in must be called at some point.
This method is also extremely useful for intermediate callbacks that need to make other asynchronous network calls before calling the user's original 'ok' or 'fail' callback functions.
- Parameters:
- {Function} newOk
- the callback Function. This function must accept three arguments including the current ok and fail function as well as any arguments specific to the callback function. By convention, the 'ok' or 'fail' functions passed in must be called after the new callback has finished.
- See:
- baja.comm.Callback#fail
Therefore, if a Batch was originally passed into this Callback object, it is assumed the caller will invoke the Batch commit method when appropriate (i.e. they've batched up all of the network requests that are going to be made).
This asynchronously makes a network call
Please note that baja.comm.Commit#commit should always be used in preference as this will result in a synchronous network call that will block everything else.
- Returns:
- {baja.comm.Batch}
- Returns:
- {Boolean}