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 Connection s |
defaultConnection |
String | The default Connection (name) for Request s |
adapters |
Object.<Object> | Adapter s that should be registered on construct |
connections |
Object.<Object> | Connection s that should be registered on construct |
requests |
Object.<Object> | Request s 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
Adapter
s on thisCommunicator
Type:
- Object
Example
communicator.adapters.XHR;
-
connections :Object
-
Object that contains all registered
Connection
s on thisCommunicator
Type:
- Object
Example
communicator.connections['local-xhr'].post('/some/relative/path/to/connection/url', data) .then(...);
-
name :String
-
Name of the
Communicator
instanceType:
- String
Example
communicator.name; // name provided to factory
-
options :Object
-
Object containing the properties provided to the
Communicator
factoryType:
- Object
Example
communicator.options; // options provided to factory
-
requests :Object
-
Object that contains all registered
Request
s on thisCommunicator
Type:
- 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
Adapter
on thisCommunicator
Parameters:
Name Type Description options
Object Object containing the properties for an Adapter
Returns:
- 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 connection
String <optional>
this.defaultConnection The name of the Connection
to 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
Connection
on thisCommunicator
Parameters:
Name Type Description options
Object Object containing the properties for a Connection
Returns:
- Type
- Connection
Example
const connection = communicator.Connection({ name: 'local-xhr', adapter: 'XHR', url: 'http://localhost:1337' });
-
Request(options) → {Request}
-
Registers an
Request
on thisCommunicator
Parameters:
Name Type Description options
Object Object containing the properties for a Request
Returns:
- Type
- Request
Example
const request = communicator.Request({ name: 'user.login', connection: 'local-xhr', route: '/user/login', method: 'get, ... });