This is a sample project showing how to create a sync adapter that integrates with IBM BlueMix - Mobile Cloud
This code utilizes the following credentials which can be found in the config.json
file
"env:development": {
"bluemix" : {
"appId" : "244246c0-bf7a-41f9-b561-f71815d33bd8",
"appSecret" : "e1567ab2d13015d9dbef414213e7ac6bdb1c8530"
}
},
We are using promises in this adapter to avoid the pain of callback hell, see more information on callbacks here Q Promises - A tool for making and composing asynchronous promises in JavaScript
We had to do some special hacking here to get promises to work in the sync adapter because of the way that controllers and models are created; basically they do not return anything after the object is created and that is where I needed to return the promise.
So if you look in the alloy.js
file in the project's root directory, you will see that I overrode the creation methods for the Collection and the Model. The only change is the addition of the return from the sync call
// line 21 & the same on line 43
return mod.sync(method, model, opts);
Titanium Version of SuperAgent helps to make the code in the sync adapter cleaner and more readable.
See Example below
#!javascript
superAgent.get(readURL)//
.set('IBM-Application-Secret', Alloy.CFG.bluemix.appSecret)//
.set('Accept', 'application/json')//
.end(function(res,error) {
console.log(res.body.object);
// do something with response...
});
The data that is returned from the REST API is not correctly formatted for the Backbone models/collections. In the Model file, we extend the objects and utilize the Backbone.Model.parse functionality to clean up the data and set the response correctly
#!javascript
function saveObject() {
var data = {
"attributes" : {
"title" : "Test With Class Name",
"address" : "1134 Buchanan St, NW",
"_geoloc" : [151.201523, -33.86887]
}
};
var model = Alloy.createModel("Location");
var $promise = model.save(data);
$promise.promise.then(function(_response) {
console.log("SUCCESS " + JSON.stringify(_response, null, 2));
}, function(_error) {
console.log("ERROR " + JSON.stringify(_error, null, 2));
});
}
#!javascript
var collection = Alloy.createCollection("Location");
// FETCH ONE ITEM BY ID WITH PROMISE
var $promise = collection.fetch({
id : "e83b4bb4692e0798d54bd385544179547493349d"
});
$promise.promise.then(function(_result) {
// get one item
console.log("Success " + JSON.stringify(_result, null, 2));
}, function(_error) {
console.log("In Error Handler " + _error);
});
#!javascript
// create the collection
var collection = Alloy.createCollection("Location");
// FETCH ONE ITEM BY ID WITH PROMISE
var $promise = collection.fetch();
$promise.promise.then(function(_result) {
// get all items, no id specified
console.log("in then " + JSON.stringify(_result, null, 2));
}, function(_error) {
console.log("in error " + _error);
});
Stuff our legal folk make us say:
Copyright (c) 2014 by Aaron K Saunders. All Rights Reserved.
Licensed under the Apache Public License (Version 2). Please see the LICENSE file for the full license.