- Notifications
You must be signed in to change notification settings - Fork0
modella/ajax
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Provides a AJAX Sync Layer formodella. Usesvisionmedia/superagent as a request library.
modella-ajax
can be used either client or server side.
To install it client side:
component install modella/modella-ajax
To install it server side:
npm install modella-ajax
As a sync layer for modella, most ofmodella-ajax
, most of its usage is abstracted away from direct usage. You simply installthe plugin and use it in modella. You must specify the API end point when using ajaxSync.
var modella = require('modella'), ajaxSync = require('modella-ajax');var User = modella('User').attr('id').attr('username');// For a local APIUser.use(ajaxSync('/users'));// Or for a remote APIUser.use(ajaxSync('http://example.com/users'));
modella-ajax
expects you to implement a RESTful API at the end-point specified. For example in the case ofUser.use(ajaxSync('/users'))
it would expect the following JSON API.
GET /users // Return a JSON list of all usersPOST /users // Creates a new user. Returns JSON of that UserGET /users/id // Return a JSON user objectPUT /users/id // Updates an existing user. Returns JSON of that userDELETE /users/id // Destroys a user
Additionally, you can "Remove all" with the following HTTP Request:
DELETE /users/ // Destroys all users
All of these methods are optional but you will not be able to use modella's depending methods without first making an APIthat responds to the appropriate patterns.
You can specify different routes than the defaults by passing in a secondoptional argument tomodella-ajax
.
The default urlMap looks like the following (and maps to the API expectations above).
var urlMap = { create: '', list: '', read: '/:primary', remove: '/:primary', removeAll: '', update: '/:primary'};
If you wanted to override them, you could do so in the following way:
var ajax = require('modella-ajax')('/api/v1/users', { read: '/:username', update: '/:username', remove: '/:username'});User.use(ajax);
This would make it so that the following routes were used:
READ -> GET /api/v1/users/:usernameUPDATE -> PUT /api/v1/users/:usernameREMOVE -> DEL /api/v1/users/:username
Emitted before XHR request is sent.
User.on('ajax request', function(req) { // req is superagent request object req.set('Authorization', 'Bearer 13a9-34b3-a8da-c78d');});
Emitted beforeModel.all()
instantiates the model instances.
User.on('ajax all', function(res) { var users = res.body.results; // Convert JSON string dates into actual dates users.forEach(u) { u.registeredAt = new Date(u.registeredAt); } res.body = users;});
Emitted beforeModel.get()
instantiates the model instance.
User.on('ajax get', function(res) { res.body.registeredAt = new Date(res.body.registeredAt);});
Emitted beforeModel.removeAll()
passes response to callback.
Emitted beforemodel.save()
passes response to callback.
Emitted beforemodel.update()
passes response to callback.
Emitted beforemodel.remove()
passes response to callback.
Worth noting that if you specify an attribute forREAD
, you must pass it inwhen querying. For example:
User.get({username: 'bobby'}, function(err, u) { }) ;
If a string is passed into get, it will try and replace:primary
with it inthe route. For Example:
User.get('1234', function(err, u) { }) ;
Wouldn't do anything because our routes wouldn't match up. You would need tospecify a route ofread: "/:primary"
.
Lastly, extra parameters passed intoModel.get
are not maintained unlessthey are in the route. For example:
User.get({username: 'tommy', age: 22})
Would still map toGET /api/v1/users/tommy
.
- allow for usage of query strings w/ modellas all method