Skip to content

Instantly share code, notes, and snippets.

@OfTheDelmer
Created November 19, 2013 19:09
Show Gist options
  • Save OfTheDelmer/7550746 to your computer and use it in GitHub Desktop.
Save OfTheDelmer/7550746 to your computer and use it in GitHub Desktop.
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .
$(function(){
/*
* @item is some id of an object we want
* @callback used on "Success" of GET for item
*/
// var getData = function(item, callback){
// return $.ajax({
// url: "/responses/"+item,
// method: "GET",
// async: true,
// success: function(data){
// callback(data)
// }
// })
// }
//console.log(getData());
// var appendItem = function(data){
// $("body").html(data["text"]);
// }
// $("body").html("loading... please be patient");
/*
* @appendItem is the callback, and it appends the TEXT to
* the body of the html
*/
// getData("something", appendItem)
/* 1. page refresh, we see, "loading..."
2.) After 10 seconds we should see response text
"Hello World!!"
*/
var getData = function(item, callback){
//callback("PROCESSED "+item);
$.ajax({
url: "/responses/"+ item,
method: "GET",
success: function(data){
callback(data["text"])
}
})
}
/*
* Should take a list of items to getData for and
* and use a callback to handle finishing the processed
* request.
*
* @items some list of things we want to process/getData for
* @callback some general callback, a function to be used later
* @doneData it builds a list of data that's been processed
* @doneCallback a function that is performed on doneData
*/
var getMany = function(items, callback, doneData, doneCallback){
var doneData = doneData || [];
var doneCallback = doneCallback || callback;
// Terminating condition
if(items.length == 0){
doneCallback(doneData);
} else {
return getData(items.shift(), function(data){
doneData.push(data)
console.log("Inner ", doneData);
getMany(items, null, doneData, doneCallback)
})
}
}
/*
* @["Larry", "Moe", "Curly"] are the items to processed
* @anonymous function the doneCallback to be called later
*/
getMany(["Larry", "Moe", "Curly"], function(data){
console.log("Outer ", data);
console.log(_.map(data, function(item){
return item.replace("PROCESSED", "FINISHED");
}))
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment