Roman Shtylman edited this page Nov 30, 2013 · 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.


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 after new instance created
READ GET /posts/<id> Model.get() or instance.fetch()
UPDATE PUT /posts/<id> 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


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.


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