Class: Communicator

Communicator

new Communicator(options)

The Communicator class serves to abstract communications with a server, it does so by executing Requests using a Connection that talks to the server using an Adapter. Options passed into the constructor will be available under the options property
Parameters:
Name Type Description
options Object Object containing the properties
Properties:
Name Type Description
Adapter Adapter The Adapter class, exposed so Adapters can be constructed, available on the instance as well as the class
Request Request The Request class, exposed so Requests can be constructed, available on the instance as well as the class
Connection Connection The Connection class, exposed so Connections can be constructed, available on the instance as well as the class
options Object Options object that was passed into the constructor
config Object Configuration for the communicator, containing properties such as 'defaultConnection'
adapters Object Hashmap containing adapters, this object will be registered using Communicator#registerAdapters
connections Object Hashmap containing connections, this object will be registered using Communicator#registerConnections
requests Object Hashmap containing requests, this object will be registered using Communicator#registerRequests
See:
Example
const communicator = new Communicator();

// register an Adapter
communicator.registerAdapter({
  name: 'XHR',
  request(request) {
    // handle request and return a promise that resolves with the server response
    // reject the promise if the request was unsuccessful
  }
});

// or

const adapter = new communicator.Adapter({
  ...
});

// register a Connection
const connection = communicator.registerConnection({
  name: 'local-xhr',
  url: 'http://localhost:1337',
  adapter: 'XHR'
});

// or

const connection = new communicator.Connection({
  ...
});

// register a Request
const request = communicator.registerRequest({
  name: 'FindByIdUsersRequest',
  shortName: 'findById',
  route: '/user/:id',
  method: 'get',
  connection: 'local-xhr'
});

// or

const request = new communicator.Request({
  ...
});

// to execute the Request using its Connection
request.execute({
    id: 4
  })
  .then(...);

// to execute a Request using a Connection
connection.request(request, {
    id: 4
  })
  .then(...);

Methods

connect(name) → {Promise}

Connects a previously registered Connection.
Parameters:
Name Type Description
name String | Connection Name of the connection (or a Connection instance) that should be connected.
Returns:
Type
Promise
Example
communicator.connect('local-xhr')
  .then(...);

disconnect(name) → {Promise}

Disconnects a Connection
Parameters:
Name Type Description
name String | Connection Name of the connection (or a Connection instance) that should be disconnected.
Returns:
Type
Promise
Example
communicator.disconnect('local-xhr')
  .then(...);

registerAdapter(adapter)

Registers a Adapter so it can be used by Connections, also available as a static method.
Parameters:
Name Type Description
adapter Object Object containing the properties for an Adapter
See:
Example
communicator.registerAdapter({...});

registerAdapters(adapters)

Registers Adapters (provided as a hashmap) so they can be used by Connections, also available as a static method.
Parameters:
Name Type Description
adapters Object.<Object> Hashmap containing objects containing the properties for Adapters
See:
Example
communicator.registerAdapters({...});

registerConnection(connection)

Register a Connection so it can be used by Requests, also available as a static method. Registers a Connection
Parameters:
Name Type Description
connection Object Object containing the properties for a Connection
See:
Example
communicator.registerConnection({...});

registerConnections(connections)

Registers Connections (provided as a hashmap) so they can be used by Requests, also available as a static method.
Parameters:
Name Type Description
connections Object.<Object> Hashmap containing objects containing the properties for Connections
See:
Example
communicator.registerConnections({...});

registerRequest(request)

Registers a Request
Parameters:
Name Type Description
request Object Object containing the properties for a Request
See:
Example
communicator.registerRequest({...});

registerRequests(requests)

Registers Requests (provided as a hashmap), also available as a static method.
Parameters:
Name Type Description
requests Object.<Object> Hashmap containing objects containing the properties for Requests
See:
Example
communicator.registerRequests({...});
Rik Hoffbauer 2015
Documentation generated by JSDoc 3.3.3 on 2015-10-20T14:38:13+02:00