REST Alloy adapter which can syncing with local SQLite database. It was developed to be as simple and fast as possible. There is no SQL sugar (you can use squel), http cache system etc. Maybe you better want to use napp.alloy.adapter.restsql if you will not have any performance issues with it.
Download latest release archive and put resty.js
in app/assets/alloy/sync/
or app/lib/alloy/sync
You can define model in simular way as with standard sql adapter:
exports.definition = {
config: {
columns: {
id: 'integer unique',
name: 'text',
api: 'text'
},
adapter: {
type: 'resty',
collection_name: 'droid',
idAttribute: 'id',
urlRoot: 'http://192.168.56.1:9081/droid/'
}
}
};
Server path must be defined in model config or as option to backbone methods (fetch
, create
, save
, destroy
). In simplest way it can be done inside model definition with urlRoot
adapter option. Read Url resolving priority if you have more complex way.
Full complex example:
exports.definition = {
config: {
columns: {
id: 'integer unique',
name: 'text',
api: 'text'
},
adapter: {
type: 'resty',
collection_name: 'droid',
idAttribute: 'id',
mode: 'RemoteFirst',
urlRoot: 'http://192.168.56.1:9081/droid/',
debug: true,
delete: true,
merge: true,
reset: false,
success: function (resp) {
},
error: function (error) {
Ti.API.warning("Request failed: " + error);
}
}
}
};
Any configuration of sync can be done in two way:
options
- parameters dict to backbone (alloy collection, model) methods (fetch
,create
,save
,destroy
).config.adapter
inside model definition
Direct options
to backbone functions take precedence over config.adapter
.
Config values with ⨏
symbol can be defined as functions. Most of them called with _.defaults(options, config.adapter)
parameter if another is not specified as (⨏) (param1, param2)
.
There are six built-in sync modes: RemoteOnly
, LocalOnly
, Remote
, Local
, RemoteFirst
, LocalFirst
.
Make request to remote server only; never read from or write to local database.
Read from or write to local database only; never make request to remote server.
Make remote request and then update local database if the request was successful.
Read from or write to local database and then send request to server. Remote request will be skipped if sync method is read
.
Read
Try to read from or write to local database. Request to remote server as fallback.
contentType (⨏, default: 'application/json' if no custom data
passed, 'application/x-www-form-urlencoded' if emulateJSON
set, else will not be set)
You can set other Titanium HTTPClient config options such as timeout
, username
, password
etc.
Server path for request can be defined in many different ways. You can find priority in which resty and backbone will try to find request url bellow. All of values can be defined as url or function.
- options.url (⨏)
- adapter.url (⨏)
- model.url (⨏: this=model) (no params) NB: in alloy<1.6.0 and backbone=0.9.2
this
can be any - options.urlRoot + model.id (⨏)
- adapter.urlRoot + model.id (⨏)
- model.urlRoot + model.id (⨏: this=model) (no params) NB: in alloy<1.6.0 and backbone=0.9.2
this
can be any - collection.url + model.id (⨏: this=collection) (no params) NB: in alloy<1.6.0 and backbone=0.9.2
this
can be any
- options.url (⨏)
- adapter.url (⨏)
- options.urlRoot (⨏)
- adapter.urlRoot (⨏)
- collection.url (⨏: this=collection) (no params) NB: in alloy<1.6.0 and backbone=0.9.2
this
can be any