backbone-dropbox.js is a sync adapter for dropbox.js. Using this sync, backbone will CRUD your models in a JSON file store in your dropbox.
The backbone-dropbox adapter can be used e.g. for single user applications like a todo-list or a feed reader.
- underscore.js 1.4.3
- backbone.js 0.9.0
- dropbox.js 0.9.2
Include the dropbox.js script:
<script src="//cdnjs.cloudflare.com/ajax/libs/dropbox.js/0.9.2/dropbox.min.js">
Create the dropbox client:
var drpbxClient = new Dropbox.Client({
key: 'YOUR_ENCRYPTED_API_KEY', sandbox: true|false
});
You can create your API key via Dropbox App Console.
If your app runs inside the browser, you have to encrypt the API key.
Now you have to authenticate. Therefor dropbox.js provides several authentication methods. For browser apps it's applicable to use
Redirect (user leaves your app and comes back):
drpbxClient.authDriver(new Dropbox.Drivers.Redirect());
or Popup:
drpbxClient.authDriver(new Dropbox.Drivers.Popup({
receiverUrl: "https://url.to/oauth_receiver.html",
rememberUser: true|false
}));
Next, overwrite Backbone.sync
Backbone.sync = DropboxSync(drpbxClient);
and let the user authenticate (via Redirect or Popup)
drpbxClient.authenticate({interactive: true}, function(error, client) {
if (error) {
throw new Error('Dropbox auth error: ' + error.status);
}
// Init/Start your app.
});
Your models and collections will now sync with your dropbox.
var myModel = Backbone.Model.extend({
store: 'myModel',
defaults: {
// ...
}
});
var myCollection = Backbone.Collection.extend({
model: myModel,
store: 'myModel'
});
While CRUDing your backbone models/collections, errors from dropbox.js may be raised. A list of common dropbox.js API errors:
- Dropbox.ApiError.INVALID_TOKEN
- Dropbox.ApiError.NOT_FOUND
- Dropbox.ApiError.OVER_QUOTA
- Dropbox.ApiError.RATE_LIMITED
- Dropbox.ApiError.NETWORK_ERROR
- Dropbox.ApiError.INVALID_PARAM
- Dropbox.ApiError.OAUTH_ERROR
- Dropbox.ApiError.INVALID_METHOD
backbone-dropbox.js is distributed under the MIT License.