-
Notifications
You must be signed in to change notification settings - Fork 0
Transport
Transports are I/O request handlers, which may use different physical means to perform their tasks. io uses an XHR as a default transport, which handles HTTP requests. An alternative fetch-based transport can be used as well.
Additionally io comes bundled with jsonp and load custom transports.
The current default transport is saved in io.defaultTransport. Custom transports are registered in io.transports, and represented by a single function (compare with Service callback function). See the main API for more details.
A custom transport to use is indicated by transport property of options object.
A transport callback is a function, which takes two parameters:
-
options— anoptionsobject described in the main API. It describes an I/O request. -
prep— a helper object made withio.prepareRequest()(see the main API).
It should return a promise.
Let's construct a fake custom transport, which always returns a constant:
define(['heya-io/io'], function (io) {
'use strict';
function transportHandler (options, prep) {
return io.Deferred.resolve(42);
}
io.transports.fake = transportHandler;
return io.makeVerb('fake', 'transport');
});This is how we use it:
io.fake('/DeepThought', {question: 'the Ultimate One'}).
then(function (result) {
console.log(
'The Answer to the Ultimate Question of Life, ' +
'The Universe, and Everything is ', result);
});