<abstract> new Adapter(options)
The
Adapter
class is an 'abstract' class, it can't be used without further implementation.
The properties below should be implemented in an implementation.
Parameters:
Name | Type | Description |
---|---|---|
options |
Object | Object containing the properties (name, connect, disconnect, subscribe, unsubscribe, upload, request) |
- See:
Properties:
Name | Type | Description |
---|---|---|
name |
String | The name of the adapter, 'XHR' for example |
connect |
function | Function that connects to the server, takes a url, should return a Promise |
disconnect |
function | Function that disconnects to the server, takes a url, should return a Promise |
subscribe |
function | Function that subscribes to a model on the server, takes a model, should return a Promise |
unsubscribe |
function | Function that unsubscribes from a model on the server, takes a model, should return a Promise |
upload |
function | Function that uploads a file to the server, takes a model, should return a Promise |
request |
function | Function that executes a request to the server, should return a Promise |
options |
Object | **SET AUTOMATICALLY** Options object passed into the constructor |
Example
// XHR implementation, // this doesn't have subscribe and unsubscribe implemented as this is not functionality of the XHR transport type const adapter = new Adapter({ name: 'XHR', connect(url) { return Promise.resolve(); }, disconnect(url) { return Promise.resolve(); }, // options consists of a 'data', 'url' and 'method' attribute, enough for $.ajax to work, as well as a 'Request' attribute, // containing the Request of this request request(options) { return new Promise((resolve, reject) => { $.ajax(options) .done((data) => { resolve(data); }) .fail((data) => { reject(data); }); }); }, // upload function designed for sails js, // sends files as multipart-form to the server in the files property upload(options) { return new Promise((resolve, reject) => { var formData = new FormData(); var xhr = new XMLHttpRequest(); _.each(options.data, (val, key) => { if (key === 'files') { if (val instanceof FileList) { _.each(val, (_val) => { formData.append('files', _val); }); } else { formData.append('files', val); } } else { formData.append(key, val); } }); xhr.open(options.method.toUpperCase(), options.url, true); xhr.send(formData); xhr.onerror = function (_data) { reject(_data); }; xhr.onload = function (_data) { resolve(_data); }; }); } });
Methods
-
<static> get(name) → {Adapter|undefined}
-
Gets an
Adapter
instance by nameParameters:
Name Type Description name
Returns:
- Type
- Adapter | undefined
-
<static> validateImplementation(options)
-
Validates if the implementation of an
Adapter
is validParameters:
Name Type Description options
-
connect(url) → {Promise}
-
Connects to a server using a full url (including protocol and port)
Parameters:
Name Type Description url
String The url of the server (including protocol and port, eg. http://some.domain.com:1337) Returns:
- Type
- Promise
-
disconnect(url) → {Promise}
-
Disconnects from a server using a full url (including protocol and port)
Parameters:
Name Type Description url
String The url of the server (including protocol and port, eg. http://some.domain.com:1337) Returns:
- Type
- Promise
-
request(request) → {Promise}
-
Makes a request to the server
Parameters:
Name Type Description request
Object Request object containing the properties listed below Properties:
Returns:
- Type
- Promise
-
subscribe(event) → {Promise}
-
Subscribes to an event on the server
Parameters:
Name Type Description event
String Event to subscribe to Returns:
- Type
- Promise
-
unsubscribe(event) → {Promise}
-
Unsubscribes from an event on the server
Parameters:
Name Type Description event
String Event to subscribe to Returns:
- Type
- Promise
-
upload(request) → {Promise}
-
Uploads files to the server.
Parameters:
Name Type Description request
Object Request object containing the properties listed below Properties:
Returns:
- Type
- Promise