Created
November 20, 2013 01:12
-
-
Save OfTheDelmer/7555801 to your computer and use it in GitHub Desktop.
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
// 1. We are going to make an ajax request. | |
// 2. We are going to GET some response TEXT. | |
// 3. We are going to append the TEXT to the DOM. | |
// Callback to wait for DOM load event | |
$(function(){ | |
// //Start ajax | |
// var getData = function(callBack){ | |
// return $.ajax({ | |
// url: "/responses/someId", | |
// method: "GET", | |
// async: false, | |
// success: function(data){ | |
// callBack(data) | |
// } | |
// }); | |
// } | |
// //console.log(getData()); | |
// var appendItem = function(data){ | |
// $("body").append(data["text"]); | |
// } | |
// getData(appendItem); | |
/* | |
* Get some Data using an ajax request | |
* @item something we're going to be processing/making | |
* making a request for | |
* @callBack some function to run afterward/later | |
*/ | |
var getData = function(item, callBack){ | |
// Send item back with PROCESSED prepended | |
//callBack("PROCESSED " + item) | |
$.ajax({ | |
url: "/responses/"+item, | |
method: "GET", | |
success: function(data){ | |
callBack(data["text"]); | |
} | |
}) | |
} | |
/* | |
* Takes a list of things and processes each item | |
* recursively | |
* | |
* @items a list of things to be processed | |
* @doneCallback what to do when finished processing items. | |
* @doneData data that has been processed and added to the | |
* list | |
*/ | |
var getMany = function(items, doneCallback, doneData){ | |
var doneData = doneData || []; | |
// Terminating condition | |
if(items.length == 0){ | |
doneCallback(doneData); | |
} else { | |
// items.shift() => returns first item in items list | |
// items now has one less item in it. | |
// So ["Larry", "Moe", "Curly"].shift() => "Larry" | |
// items => ["Moe", "Curly"] | |
// getData("Larry", anonFunc) | |
getData(items.shift(), function(data){ | |
doneData.push(data); | |
console.log("Inner @data: ", data); | |
console.log("Inner @doneData: ", doneData); | |
getMany(items,doneCallback, doneData); | |
}) | |
} | |
} | |
getMany(["Larry", "Moe", "Curly"], function(data){ | |
console.log("Outer @doneCallback: ", data); | |
console.log(stuff = _.map(data, function(item){ | |
return item.replace("PROCESSED", "FINISHED"); | |
})) | |
$("body").append(stuff.join()) | |
}) | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment