{
"name": "JSON APIs and Ajax",
"order": 0.0065,
"challenges": [
{
"id": "bb000000000000000000001",
"title": "Trigger on click Events with jQuery",
"difficulty": 3.19,
"description": [
"With jQuery we are able to get data from APIs via Ajax.",
"This data normally comes in the form of JSON.",
"Let's get the Get Message
button to set the text of a div.",
"We will later use this to display the result of out API request.",
"$(\"#getMessage\").on(\"click\", function(){
",
" $(\".message\").html(\"Here is the message\");
",
"});
"
],
"tests": [
"assert(editor.match(/\\$\\s*?\\(\\s*?(?:'|\")\\#getMessage(?:'|\")\\s*?\\)\\s*?\\.on\\s*?\\(\\s*?(?:'|\")click(?:'|\")\\s*?\\,\\s*?function\\s*?\\(\\s*?\\)\\s*?\\{/gi), 'You should have bound the click event to the getMessage button.')",
"assert(editor.match(/\\$\\s*?\\(\\s*?(?:'|\")\\.message(?:'|\")\\s*?\\)\\s*?\\.html\\s*?\\(\\s*?(?:'|\")Here\\sis\\sthe\\smessage(?:'|\")\\s*?\\);/gi), 'You should set te value of the #message box to be the message given in the description.')",
"assert(editor.match(/\\n*?\\s*?\\}\\n*?\\s*?\\);/gi) && editor.match(/\\n*?\\s*?\\}\\);/gi).length >= 2, 'Make sure that you close off all of your functions.')"
],
"challengeSeed": [
"fccss",
" $(document).ready(function() {",
" ",
" });",
"fcces",
"",
"",
"",
"
$(\"#getMessage\").on(\"click\", function() {",
" $.getJSON(\"/json/cats.json?callback=\", function( json ) {",
" //Code to run when request is complete",
" $(\".message\").html(JSON.stringify(json))",
" });",
"});
",
"Let's make it so that the data sent from the request is appended to the .message div.",
""
],
"tests": [
"assert(editor.match(/\\$\\s*?\\(\\s*?(\\\"|\\')\\#getMessage(\\\"|\\')\\s*?\\)\\s*?\\.\\s*?on\\s*?\\(\\s*?(\\\"|\\')click(\\\"|\\')\\s*?\\,\\s*?function\\s*?\\(\\s*?\\)\\s*?\\{/gi), 'You should have a click handler on the getMessage button to trigger the AJAX request.')",
"assert(editor.match(/\\s*?\\}\\s*?\\)\\s*?\\;/gi), 'You should have at least on closing set of brackets and parenthesis.')",
"assert(editor.match(/\\s*?\\}\\s*?\\)\\s*?\\;/gi) && editor.match(/\\,\\s*?function\\s*?\\(\\s*?\\w*?\\s*?\\)\\s*?\\{/gi) && editor.match(/\\s*?\\}\\s*?\\)\\s*?\\;/gi).length === editor.match(/\\s*?function\\s*?\\(\\s*?\\w*?\\s*?\\)\\s*?\\{/gi).length, 'Each callback function should have a closing set of brackets and parenthesis.')",
"assert(editor.match(/\\$\\s*?\\.\\s*?getJSON\\s*?\\(\\s*?\"\\\/json\\\/cats\\.json\\?callback\\=\"\\s*?\\,\\s*?function\\s*?\\(\\s*?json\\s*?\\)\\s*?\\{/gi), 'You should be making use of the getJSON method given in the description to load data from the json file.')",
"assert(editor.match(/\\$\\s*?\\(\\s*?\\\"\\.message\\\"\\s*?\\)\\s*?\\.\\s*?html\\s*?\\(\\s*?JSON\\s*?\\.\\s*?stringify\\s*?\\(\\s*?json\\s*?\\)\\s*?\\)/gi), 'Don\\'t forget to make the .html
change the contents of the message box so that it contains the result of the getJSON.')"
],
"challengeSeed": [
"fccss",
" $(document).ready(function() {",
" ",
" $(\"#getMessage\").on(\"click\", function(){",
" $(\".message\").html(\"Make the result of the getJSON request appear here\")",
" });",
" ",
" });",
"fcces",
"",
"",
"",
"",
" json.map(function(val){",
" ",
" html = html + \"<div class = 'cat'>\"",
" ",
" for(var key in val){",
" html = html + '<div class = \"' + key + '\">' + val[key] + '</div>';",
" }",
" ",
" html = html + \"</div><br/>\"",
" ",
" });",
"
",
""
],
"tests": [
"assert(/json\\.map/gi, 'The message box should have something in it.')"
],
"challengeSeed": [
"fccss",
" $(document).ready(function() {",
" ",
" $(\"#getMessage\").on(\"click\", function() {",
" $.getJSON(\"/json/cats.json?callback=\", function( json ) {",
" ",
" var html = \"\";",
" ",
" //Add you code to modify the data here. It should add it to the html sting for use in the view",
" ",
" //Don't modify above here",
" ",
" ",
" ",
" //Don't modify below here",
" ",
" $(\".message\").html(html);",
" ",
" });",
" });",
" ",
" });",
"fcces",
"",
"",
"",
"",
"if(key === \"imageLink\"){",
"html = html + '<img class = \"' + key + '\"src = \"' + val[key] + '\">';",
"}",
"else{",
" html = html + '<div class = \"' + key + '\">' + val[key] + '</div>';",
"}",
"
",
""
],
"tests": [
"assert(editor.match(/imageLink/gi), 'You should have accessed the imageLink of each cat object.')"
],
"challengeSeed": [
"fccss",
"$(document).ready(function() {",
" ",
" $(\"#getMessage\").on(\"click\", function() {",
" $.getJSON(\"/json/cats.json?callback=\", function( json ) {",
" ",
" var html = \"\";",
" ",
" //Add you code to modify the data here. It should add it to the html sting for use in the view",
" ",
" json.map(function(val){",
"",
" html = html + \"",
"json = json.filter(function(val){",
" return(val.id !== 1);",
"});",
"
",
""
],
"tests": [
"assert(editor.match(/filter/gi), 'You should be making use of the .filter method.')"
],
"challengeSeed": [
"fccss",
"$(document).ready(function() {",
" ",
" $(\"#getMessage\").on(\"click\", function() {",
" $.getJSON(\"/json/cats.json?callback=\", function( json ) {",
" ",
" var html = \"\";",
" ",
" //Add you code to modify the data here. It should add it to the html sting for use in the view",
" ",
" json.map(function(val){",
"",
" val = \"\" ",
"",
" html = html + \"",
"if (navigator.geolocation) {",
" navigator.geolocation.getCurrentPosition(function(position){",
" // Do something in here with the coordinates!",
" ",
" $(\"#data\").html(\"latitiude\" + position.coords.latitude + \"longitude\" + position.coords.longitude);",
" ",
" });",
"}",
"
"
],
"tests": [
"assert(editor.match(/navigator\\.geolocation\\.getCurrentPosition/gi), 'you should make use of the navigator.geolocation
to access the users current location.')"
],
"challengeSeed": [
"fccss",
"",
"fcces",
"