Skip to content

Instantly share code, notes, and snippets.

@rominirani
Last active December 21, 2015 05:18
Show Gist options
  • Save rominirani/6255563 to your computer and use it in GitHub Desktop.
Save rominirani/6255563 to your computer and use it in GitHub Desktop.
Episode #10 : Firefox OS Tutorial : Using mBaaS - app.js
var quotes = [];
var authors = [];
function mBaasInit() {
var promise;
promise = Kinvey.init({
appKey : 'YOUR_APP_KEY',
appSecret : 'YOUR_APP_SECRET'
});
promise.then(function(activeUser) {
console.log("All is well in Kinveyland");
populateAuthors();
}, function(error) {
alert("Could not initialize Kinvey");
});
}
function populateAuthors() {
var promise = Kinvey.DataStore.find('Authors', null, {
success: function(response) {
var result = "";
authors = [];
for (var i=0;i <response.length; i++) {
var author = response[i];
authors.push(author);
$("#authorList").append('<li>' + author.name + '</li>').listview('refresh');
}
}
});
}
function populateQuotes(author) {
var query = new Kinvey.Query();
query.equalTo('who', author);
var p = Kinvey.DataStore.find('Quotes', query, {
success: function(response) {
var result = "";
quotes = [];
for (var i=0;i <response.length; i++) {
var quote = response[i];
quotes.push(quote);
}
//Change the page now
$.mobile.changePage($("#quotespage"));
}
});
}
$(document).bind('pageinit', function() {
$('#authorList').empty();
$("#authorList").listview('refresh');
//Initialize Kinvey
mBaasInit();
//When an Author List Element is clicked, fetch their quotes
$(document).on('click', '#authorList li', function(){
populateQuotes($(this).text());
});
//When quotespage is live, clear the current quotes and populate the new ones
$('#quotespage').live('pageshow',function(){
$('#quotesList').empty();
for(var i=0;i<quotes.length;i++) {
$('#quotesList').append('<li>' + quotes[i].what + '</li>');
$('#quotesList').listview('refresh');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment