Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Ajax Sync Layer for Modella

NotificationsYou must be signed in to change notification settings

modella/ajax

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Provides a AJAX Sync Layer formodella. Usesvisionmedia/superagent as a request library.

Installation

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

Usage

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'));

API Expectations

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.

Defining Alternative Routes

You can specify different routes than the defaults by passing in a secondoptional argument tomodella-ajax.

Defaults

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'};

Overriding

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

Events

ajax request

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');});

ajax all

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;});

ajax get

Emitted beforeModel.get() instantiates the model instance.

User.on('ajax get', function(res) {  res.body.registeredAt = new Date(res.body.registeredAt);});

ajax removeAll

Emitted beforeModel.removeAll() passes response to callback.

ajax save

Emitted beforemodel.save() passes response to callback.

ajax update

Emitted beforemodel.update() passes response to callback.

ajax remove

Emitted beforemodel.remove() passes response to callback.

Gotchyas

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.

Todo

  • allow for usage of query strings w/ modellas all method

About

Ajax Sync Layer for Modella

Resources

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp