new Connection(options)
Parameters:
Name |
Type |
Description |
options |
Object
|
Object containing the properties (name, adapter and url) |
Properties:
Name |
Type |
Description |
name |
String
|
Name of the connection, 'local-xhr' for example |
adapter |
String
|
Reference to the name of an Adapter , 'XHR' for example, the Connection will use this adapter to execute Request s |
url |
String
|
The base url of the connection, including protocol and port (if necessary), 'http://localhost:1337' for example |
options |
Object
|
**SET AUTOMATICALLY** Options passed into the constructor |
state |
enums/CONNECTION_STATE
|
**SET AUTOMATICALLY** The connection state of the Connection |
connected |
Boolean
|
**SET AUTOMATICALLY** Boolean indicating whether this Connection is connected |
disconnected |
Boolean
|
**SET AUTOMATICALLY** Boolean indicating whether this Connection is disconnected |
connecting |
Boolean
|
**SET AUTOMATICALLY** Boolean indicating whether this Connection is connecting |
- See:
-
Example
const connection = new Connection({
name: 'local-xhr',
url: 'http://localhost:1337',
adapter: 'XHR'
});
// execute Requests
connection.request(request, data)
.then(...);
// execute undefined request
connection.get('/some/route/:id', data)
.then(...);
connection.post('/some/route/:id', data)
.then(...);
connection.put('/some/route/:id', data)
.then(...);
connection.delete('/some/route/:id', data)
.then(...);
// subscribe to server events
connection.subscribe('user', (ev) => {
// event received from server
console.log(ev);
});
// unsubscribe from server events
connection.unsubscribe('user');
// listen for events on the connection
connection.on('connect', () => {
});
connection.on('connectFail', () => {
});
// listen for custom events
connection.on('someEvent', (data) => {
});
// trigger custom events
connection.trigger('someEvent', data);
-
<static> get(name) → {Connection|undefined}
-
Parameters:
Name |
Type |
Description |
name |
|
|
Returns:
-
Type
-
Connection
|
undefined
-
<static> registerAdapter(adapter) → {Adapter}
-
Parameters:
Name |
Type |
Description |
adapter |
Object
|
object containing properties for Adapter |
Returns:
-
Type
-
Adapter
Example
Connection.registerAdapter({
name: 'someAdapter',
...
});
-
<static> registerAdapters(adapters) → {Object.<Adapter>}
-
Registers
Adapter
s, provided as a hashmap Object
Parameters:
Name |
Type |
Description |
adapters |
Object.<Object>
|
Hashmap containing properties for Adapter s |
Returns:
-
Type
-
Object.<Adapter>
Example
Connection.registerAdapters({
someAdapter: {
...
}
});
-
<static> validateImplementation(options, options)
-
Validates an implementation of a
Connection
(a POJO containing the properties), throws an Error when a validation error occurs.
Parameters:
Name |
Type |
Description |
options |
Object
|
The implementation to validate |
options |
|
|
Throws:
Error
-
connect() → {Promise}
-
Returns:
-
Type
-
Promise
Example
connection.connect()
.then(...);
-
delete(route, data) → {Promise}
-
Parameters:
Name |
Type |
Description |
route |
String
|
Route of the request, splats will be filled with data from the data parameter |
data |
*
|
Data to send with the request and fill splats in the route with |
Returns:
-
Type
-
Promise
Example
connection.delete('/user/:splat', {splat: 3})
.then(...);
-
disconnect() → {Promise}
-
Returns:
-
Type
-
Promise
Example
connection.disconnect()
.then(...);
-
get(route, data) → {Promise}
-
Parameters:
Name |
Type |
Description |
route |
String
|
Route of the request, splats will be filled with data from the data parameter |
data |
*
|
Data to send with the request and fill splats in the route with |
Returns:
-
Type
-
Promise
Example
connection.get('/user/:splat', {splat: 3})
.then(...);
-
on(event, cb)
-
Listens for an event
Parameters:
Name |
Type |
Description |
event |
String
|
Event to listen to |
cb |
function
|
Function to call when event has occurred |
Example
connection.on('someEvent', data => {
// ...
});
-
post(route, data) → {Promise}
-
Parameters:
Name |
Type |
Description |
route |
String
|
Route of the request, splats will be filled with data from the data parameter |
data |
*
|
Data to send with the request and fill splats in the route with |
Returns:
-
Type
-
Promise
Example
connection.post('/user/:splat', {splat: 3})
.then(...);
-
put(route, data) → {Promise}
-
Parameters:
Name |
Type |
Description |
route |
String
|
Route of the request, splats will be filled with data from the data parameter |
data |
*
|
Data to send with the request and fill splats in the route with |
Returns:
-
Type
-
Promise
Example
connection.put('/user/:splat', {splat: 3})
.then(...);
-
registerRequest(request) → {Request}
-
Parameters:
Name |
Type |
Description |
request |
Object
|
Object containing the properties for the Request |
- See:
-
Returns:
-
Type
-
Request
Example
connection.registerRequest({...});
-
registerRequest(requests) → {Object.<Request>}
-
Registers a request for this connection, the
Request
will become available under this.requests[request.shortName].
Parameters:
Name |
Type |
Description |
requests |
Object.<Object>
|
Hashmap containing objects containing the properties for the Request |
- See:
-
Returns:
-
Type
-
Object.<Request>
Example
connection.registerRequests({
UserLoginRequest: {
shortName: 'login',
method: 'get',
...
}
});
-
request(request, data) → {Promise}
-
Parameters:
- See:
-
Returns:
-
Type
-
Promise
Example
connection.request(request, data)
.then(...)
-
subscribe(event, cb) → {Promise}
-
Subscribe to a server event
Parameters:
Name |
Type |
Description |
event |
String
|
Event to subscribe to |
cb |
function
|
Function to execute when the event has been received from the server |
Returns:
-
Type
-
Promise
Example
connection.subscribe('event', data => {
// ...
})
.then(...);
-
trigger(event, data)
-
Triggers an event with data
Parameters:
Name |
Type |
Description |
event |
String
|
Event to trigger |
data |
*
|
Data to pass into the event handler(s) |
Example
connection.trigger('someEvent', data);
-
unsubscribe(event) → {Promise}
-
Remove all subscriptions from an event
Parameters:
Name |
Type |
Description |
event |
String
|
Event to unsubscribe from |
Returns:
-
Type
-
Promise
Example
connection.unsubscribe('event')
.then(...);