Last active
August 29, 2015 14:03
-
-
Save aaronksaunders/e9c8822c577ab2db2839 to your computer and use it in GitHub Desktop.
Appcelerator Cloud Services, Promises and Custom Objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
var Q = require('q'); // add q.js library - https://github.com/kriskowal/q | |
var Cloud = require("ti.cloud"); // add in tiapp.xml | |
Cloud.debug = true; | |
function cloudCreateObjects(_data) { | |
var deferred = Q.defer(); | |
Cloud.Objects.create({ | |
classname : 'cars', | |
fields : _data | |
}, function(e) { | |
if (e.success) { | |
deferred.resolve(e.cars[0]); | |
} else { | |
Ti.API.error('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); | |
deferred.reject(e); | |
} | |
}); | |
return deferred.promise; | |
} | |
function createObject(_data) { | |
var deferred = Q.defer(); | |
setTimeout(function() { | |
deferred.resolve(_data); | |
}, 200); | |
return deferred.promise; | |
} | |
function processData() { | |
var deferred = Q.defer(); | |
var data = [{ | |
make : "nissan1", | |
year : "2005" | |
}, { | |
make : "honda1", | |
year : "2006" | |
}]; | |
Q.all(data.map(function(params) { | |
var current = cloudCreateObjects(params).then(function(result) { | |
return result; | |
}); | |
return current; | |
})).then(function(results) { | |
// results is an array of makes | |
Ti.API.debug("Success Array: " + JSON.stringify(results, null, 2)); | |
}, function(_error) { | |
// error results is an array of makes | |
Ti.API.error(_error); | |
}); | |
} | |
// | |
// LOGIN USER TO START PROCESS | |
// | |
Cloud.Users.login({ | |
login : "test@mycompany.com", // create user in appcelerator cloud services | |
password : "password" | |
}, function(e) { | |
if (e.success) { | |
var user = e.users[0]; | |
processData(); | |
} else { | |
Ti.API.error(e); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment