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 Adapter s can be constructed, available on the instance as well as the class |
Request |
Request | The Request class, exposed so Request s can be constructed, available on the instance as well as the class |
Connection |
Connection | The Connection class, exposed so Connection s 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
Adapter
Parameters:
Name Type Description adapter
Object Object containing the properties (implementation) for the Adapter
- See:
Returns:
- Type
- Adapter
Example
ClassWithConnection.registerAdapter({ name: 'XHR', ... });
-
<static> registerAdapters(adapters) → {Object.<Adapter>}
-
Registers
Adapter
sParameters:
Name Type Description adapters
Object.<Object> Hashmap containing objects containing the properties for the Adapter
s- See:
Returns:
- Type
- Object.<Adapter>
Example
ClassWithConnection.registerAdapter({ 'XHR': { ... } });
-
<static> registerConnection(connection) → {Connection}
-
Registers a
Connection
Parameters:
Name Type Description connection
Object 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
Connection
sParameters:
Name Type Description connections
Object.<Object> Hashmap containing objects containing the properties for the Connection
s- 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
ClassWithConnection
implementation. 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 options
Object Object containing the implementation for a ClassWithConnection
Throws:
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 theConnection
as data).Parameters:
Name Type Description name
String 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 event
String The event to listen to cb
function Function that should be called when this event is triggered Example
instance.on('event', (data) => { // handle event });
-
registerRequest(request) → {Request}
-
Registers a
Request
for this class, theRequest
will become available under this.requests[request.shortName].Parameters:
Name Type Description request
Object 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
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
instance.registerRequests({ UserLoginRequest: { shortName: 'login', method: 'get', ... } });
-
trigger(event, data)
-
Triggers an event
Parameters:
Name Type Description event
String The event to trigger data
* Data to pass to the event handlers Example
instance.trigger('event', data);