Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Sync

Roman Shtylman edited this page · 1 revision

The sync function provides persistence for bamboo models to/from a data store via CRUD operations.

The sync function takes two argumentes: options and callback. The options argument is an object describing the operation and the callback will be called with a result by your sync function upon completion of the operation.

options

The options object is created by bamboo and contains the following field.

url {String}

Full path to url resource. For create operations, this will be the url_root of the model, for other operations this will be url_root/<id>

method {String}

Type of operation being performed. This will be an HTTP-ish verb but the sync function does not have to use ajax.

The following table outlines the method, expected operation, and example url (assuming url_root of '/posts')

operation method example url invoked by
CREATE POST /posts instance.save() after new instance created
READ GET /posts/<id> Model.get() or instance.fetch()
UPDATE PUT /posts/<id> instance.save() for existing instances
DELETE DELETE /posts/<id> instance.destroy() for existing instances

query {Object}

When using the Model.find() function to locate specific instances with an optional query, the sync function will be called with the following.

operation method example url invoked by
FIND GET /posts?query Model.find()

body {Object}

The body option will contain the schema fields. The body field is present for the following operations: POST, PUT

callback(err, result)

The callback function should be called by your sync function upon completion (success or failure) of the sync operation.

The two arguments you must pass to the callback function are: error and result

error

The error argument should be an instance of the javascript Error type. The error argument can optionally have a status property and a message property.

The status property is a subset of http status codes:

status description
400 bad request
403 forbidden
404 not found
500 other error

If your ajax results contain other codes, they should be normalized to the above or you can pass them as is. The error is passed through bamboo to your code so you can add other properties.

result

The result will depend on the method for sync. The table below describes the result values for the different methods. Some methods do not have a result and you should callback with null for the result value.

operation method result
FIND GET Array of js objects
CREATE POST Object with id property. id is the new resource id.
READ GET Object with all properties for resouces (including id)
UPDATE PUT no result
DELETE DELETE no result
Something went wrong with that request. Please try again.