new ClassWithConnection()
The
ClassWithConnection class is there for you to extend.
It allows you to create classes that know how to connect to a server using a Connection.
- See:
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 | Object passed into the constructor |
connected |
Boolean | Indicates whether the Connection has connected successfully |
connection |
Connection | The Connection for this class, only available once connected. |
Example
class SomeClass extends ClassWithConnection {
constructor(options = {}) {
options.connection = 'some-registered-connection';
options.adapters = {
...
};
options.connections = {
...
};
options.requests = {
...
};
super(options);
this.on('connect', () => {
// do some stuff
});
}
initialize() {
// if implemented, the initialize method will be called on connect
}
}
someInstance.on('connect', () => {
someInstance.connection.get('/someRoute');
});
Methods
-
<static> registerAdapter(adapter) → {Adapter}
-
Registers an
AdapterParameters:
Name Type Description adapterObject Object containing the properties (implementation) for the Adapter- See:
Returns:
- Type
- Adapter
Example
ClassWithConnection.registerAdapter({ name: 'XHR', ... }); -
<static> registerAdapters(adapters) → {Object.<Adapter>}
-
Registers
AdaptersParameters:
Name Type Description adaptersObject.<Object> Hashmap containing objects containing the properties for the Adapters- See:
Returns:
- Type
- Object.<Adapter>
Example
ClassWithConnection.registerAdapter({ 'XHR': { ... } }); -
<static> registerConnection(connection) → {Connection}
-
Registers a
ConnectionParameters:
Name Type Description connectionObject Object containing the properties for the Connection- See:
Returns:
- Type
- Connection
Example
ClassWithConnection.registerConnection({ name: 'local-xhr', adapter: 'XHR', url: 'http://localhost:1337' }); -
<static> registerConnections(connections) → {Object.<Connection>}
-
Registers
ConnectionsParameters:
Name Type Description connectionsObject.<Object> Hashmap containing objects containing the properties for the Connections- See:
Returns:
- Type
- Object.<Connection>
Example
ClassWithConnection.registerConnection({ 'local-xhr': { adapter: 'XHR', url: 'http://localhost:1337' } }); -
<static> validateImplementation(options)
-
Validates whether an object contains all properties to be considered a valid
ClassWithConnectionimplementation. A valid configuration consists of a connection property that is a string, and maps onto aConnection's name that has been registeredParameters:
Name Type Description optionsObject Object containing the implementation for a ClassWithConnectionThrows:
Error -
connect(name)
-
Connects this class to a server using a
Connection, when connected the connection becomes available under this.connection and the 'connect' event is triggered (with theConnectionas data).Parameters:
Name Type Description nameString Name of the connection to connect to, defaults to this.options.connection Example
instance.connect('local-xhr') .then(...); -
on(event, cb)
-
Listens for an event
Parameters:
Name Type Description eventString The event to listen to cbfunction Function that should be called when this event is triggered Example
instance.on('event', (data) => { // handle event }); -
registerRequest(request) → {Request}
-
Registers a
Requestfor this class, theRequestwill become available under this.requests[request.shortName].Parameters:
Name Type Description requestObject Object containing the properties for the Request- See:
Returns:
- Type
- Request
Example
instance.registerRequest({...}); -
registerRequest(requests) → {Object.<Request>}
-
Registers a request for this class, the
Requestwill become available under this.requests[request.shortName].Parameters:
Name Type Description requestsObject.<Object> Hashmap containing objects containing the properties for the Request- See:
Returns:
- Type
- Object.<Request>
Example
instance.registerRequests({ UserLoginRequest: { shortName: 'login', method: 'get', ... } }); -
trigger(event, data)
-
Triggers an event
Parameters:
Name Type Description eventString The event to trigger data* Data to pass to the event handlers Example
instance.trigger('event', data);