Skip to content

Instantly share code, notes, and snippets.

@flexgrip
Last active December 22, 2015 13:58
Show Gist options
  • Save flexgrip/6482450 to your computer and use it in GitHub Desktop.
Save flexgrip/6482450 to your computer and use it in GitHub Desktop.
Get data from Drupal Services rest server to the Intel XDK. Yeah yeah yeah.
/*
_ _ _______ ______
| \ | | ____\ \ / / ___|
| \| | _| \ \ /\ / /\___ \
| |\ | |___ \ V V / ___) |
|_| \_|_____| \_/\_/ |____/
================================*/
function getBlog() {
// Pull the curtain
$("#loadingdata").show();
// Set the URL to retreive
urlNews = services_endpoint + "/api/views/blog_api.jsonp?something=nothing";
// Make the request and pass the results to the handlers
AppMobi.device.getRemoteData(urlNews, "GET","","news_handler","news_error_handler");
}
function news_handler(data) {
data = JSON.parse(data);
//console.log(data);
// Sort the articles by distance
data.sort(distanceSort);
var newsitems = "";
count = 0;
// Let's do everything in long form until we compress the app itself
// Check to see if the data was empty (this is not a feature of the Intel XDK)
if (data.length == 0) {
$("#news-container").html("No news articles at this time.");
// If so, Hide the loading spinner
$("#loadingdata").hide();
} else {
$.each(data, function(i,item){
count++;
// Define the json labels
var nid = item.nid;
var body = item.body;
var title = item.title;
var image = item.image;
/*** BUILD NEWS ITEMS***/
// Do it manually since the templating engine is super slow and buggy
var newsitem = '<div class="blog-post"><div class="blog-post-title"><h3>'+title+'</h3></div><div class="blog-post-body">'+body+'</div><div class="blog-post-image">'+image+'</div></div>';
// Add it all up
newsitems += newsitem;
});
$("#news-container").html(newsitems);
// CREATE a detailed panel for each article
// SHOULD THIS BE MOVED TO CALLBACK? Probably.
// Look into this when you need to optimize everything.
$("#loadingdata").hide();
};
}
function news_error_handler() {
console.log("Couldn't load blog posts!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment