new Communicator(options)
Parameters:
| Name | Type | Description |
|---|---|---|
options |
Object | Object containing the properties listed below |
- To Do:
-
- validate options
Properties:
| Name | Type | Description |
|---|---|---|
defaultAdapter |
String | The default Adapter (name) for Connections |
defaultConnection |
String | The default Connection (name) for Requests |
adapters |
Object.<Object> | Adapters that should be registered on construct |
connections |
Object.<Object> | Connections that should be registered on construct |
requests |
Object.<Object> | Requests that should be registered on construct |
Example
import Communicator from 'frontend-communicator';
const communicator = Communicator({
name: 'some-unique-name-for-the-communicator',
defaultConnection: 'local-xhr',
defaultAdapter: 'XHR',
adapters: {...},
connections: {...},
requests: {...}
});
export default communicator;
Members
-
adapters :Object
-
Object that contains all registered
Adapters on thisCommunicatorType:
- Object
Example
communicator.adapters.XHR;
-
connections :Object
-
Object that contains all registered
Connections on thisCommunicatorType:
- Object
Example
communicator.connections['local-xhr'].post('/some/relative/path/to/connection/url', data) .then(...); -
name :String
-
Name of the
CommunicatorinstanceType:
- String
Example
communicator.name; // name provided to factory
-
options :Object
-
Object containing the properties provided to the
CommunicatorfactoryType:
- Object
Example
communicator.options; // options provided to factory
-
requests :Object
-
Object that contains all registered
Requests on thisCommunicatorType:
- Object
Example
communicator.requests['local-xhr'].user.login.execute('/some/relative/path/to/connection/url', data) .then(...); -
servers :Object
-
Type:
- Object
Example
communicator.servers['local-xhr'].user.login() .then(...);
Methods
-
Adapter(options) → {Adapter}
-
Registers an
Adapteron thisCommunicatorParameters:
Name Type Description optionsObject Object containing the properties for an AdapterReturns:
- Type
- Adapter
Example
const adapter = communicator.Adapter({ name: 'XHR', connect() {...}, disconnect() {...}, request() {...}, ... }); -
connect(connection) → {Promise}
-
Connects to a specified connection
Parameters:
Name Type Argument Default Description connectionString <optional>
this.defaultConnection The name of the Connectionto connect to.Returns:
A promise that resolves when the connection has been established- Type
- Promise
Example
communicator.connect('local-xhr') .then(...); // or, when having set a defaultConnection, communicator.defaultConnection = 'local-xhr'; communicator.connect() .then(...); -
Connection(options) → {Connection}
-
Registers an
Connectionon thisCommunicatorParameters:
Name Type Description optionsObject Object containing the properties for a ConnectionReturns:
- Type
- Connection
Example
const connection = communicator.Connection({ name: 'local-xhr', adapter: 'XHR', url: 'http://localhost:1337' }); -
Request(options) → {Request}
-
Registers an
Requeston thisCommunicatorParameters:
Name Type Description optionsObject Object containing the properties for a RequestReturns:
- Type
- Request
Example
const request = communicator.Request({ name: 'user.login', connection: 'local-xhr', route: '/user/login', method: 'get, ... });