blob: 800b1e0df4ba7c329da0c2ec56ec6222b4634bc1 [file] [log] [blame]
$(document).ready(function () {
//call the runner function to start the process
$('#start-button').bind('click', function() {
$('#start-button').attr("disabled", "disabled");
$('#test-output').html('');
runner(0);
});
var logSuccess = true;
var successCount = 0;
var logError = true;
var errorCount = 0;
var logNotice = true;
var client = new Usergrid.Client({
orgName:'yourorgname',
appName:'sandbox',
logging: true, //optional - turn on logging, off by default
buildCurl: true //optional - turn on curl commands, off by default
});
function runner(step, arg, arg2){
step++;
switch(step)
{
case 1:
notice('-----running step '+step+': create and serialize collection');
createAndSerialzeCollection(step);
break;
case 2:
notice('-----running step '+step+': de-serialize collection');
deserializeCollection(step);
break;
default:
notice('-----test complete!-----');
notice('Success count= ' + successCount);
notice('Error count= ' + errorCount);
notice('-----thank you for playing!-----');
$('#start-button').removeAttr("disabled");
}
}
//logging functions
function success(message){
successCount++;
if (logSuccess) {
console.log('SUCCESS: ' + message);
var html = $('#test-output').html();
html += ('SUCCESS: ' + message + '\r\n');
$('#test-output').html(html);
}
}
function error(message){
errorCount++
if (logError) {
console.log('ERROR: ' + message);
var html = $('#test-output').html();
html += ('ERROR: ' + message + '\r\n');
$('#test-output').html(html);
}
}
function notice(message){
if (logNotice) {
console.log('NOTICE: ' + message);
var html = $('#test-output').html();
html += (message + '\r\n');
$('#test-output').html(html);
}
}
function createAndSerialzeCollection(step){
var options = {
type:'books',
qs:{ql:'order by name'}
}
client.createCollection(options, function (err, books) {
if (err) {
error('could not make collection');
} else {
//collection made, now serialize and store
localStorage.setItem('item', books.serialize());
success('new Collection created and data stored');
runner(step);
}
});
}
function deserializeCollection(step){
var books = client.restoreCollection(localStorage.getItem('item'));
while(books.hasNextEntity()) {
//get a reference to the book
book = books.getNextEntity();
var name = book.get('name');
notice('book is called ' + name);
}
success('looped through books');
runner(step);
}
});