diff --git a/README.md b/README.md index 6343b73..9736693 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Introduce jQuery. Discuss its role as a javascript library (not a different lang ### Class 4 -Introduce jQuery UI plugins and a lightbox plugin. Students will build a portfolio of images with a slideshow and jqueryUI elements. +Introduce APIs, REST, and AJAX. Students will interact with the Meetup API (chosen because all students are already aware of Meetup and calls can be made without oAuth authentication). Bonus intros to jQuery UI, and the concept of jQuery plugins ## Theme customization diff --git a/class3.html b/class3.html index dcd3169..33c94d0 100644 --- a/class3.html +++ b/class3.html @@ -40,7 +40,7 @@

Beginning Javascript

-

Class 2

+

Class 3

diff --git a/class4.html b/class4.html new file mode 100644 index 0000000..84a52f6 --- /dev/null +++ b/class4.html @@ -0,0 +1,398 @@ + + + + + + + Class 4 ~ Javascript ~ Girl Develop IT + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+ +
+ +

Beginning Javascript

+

Class 4

+
+ + +
+

Welcome!

+
+

Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.

+

Some "rules"

+
    +
  • We are here for you!
  • +
  • Every question is important
  • +
  • Help each other
  • +
  • Have fun
  • +
+
+
+ +
+

Warning

+
+ The following few slides have lots of acronyms and jargon. +
+
On behalf of developers everywhere, we apologize
+
+ +
+

What is an API?

+
    +
  • Application Programming Interface
  • +
  • Data structure and rules for accessing a web-based appllication
  • +
  • How we can access information from sites that are not our own (Twitter, Meetup, Facebook, Foursquare)
  • +
+
+ +
+

What is an API?

+
    +
  • Primary role: a channel for applications to work together +
      +
    • Your website and the Twitter API
    • +
    • Twitter's mobile app and the Twitter API
    • +
    • Hootsuite's mobile app and the Twitter API
    • + +
    +
+ + +
+

What is AJAX?

+
    +
  • Asynchronous JavaScript and XML
  • +
  • Method to communicate to a server or API
  • +
  • Asynchronous means: +
      +
    • I ask Twitter for all the tweets ever!
    • +
    • That will take a while
    • +
    • My whole website could be locked up while I wait!
    • +
    • Or, my call can be 'asynchronous' and my website will just listen for Twitter's response with one ear, but go about normal business until the response arrives. +
    +
  • +
  • Requests and results can be in JavaScript or XML
  • +
+
+ + +
+

What is REST?

+
    +
  • Representational State Transfer
  • +
  • REST is a way to ask an API for information by using a URL.
  • +
  • REST Urls are created with the following syntax: +
      +
    • http://ApiSite.com/method?parameter=value&parameter=value
    • +
    • Method -- what you want from the API. Defined by API documentations
    • +
    • Parameter -- a type of filter or restraint. Defined by API documentations
    • +
    • Value -- value for parameter. Defined by you!
    • +
    +
  • + +
+
+ +
+

Where do I learn about an API?

+

All (good) APIs have documentation

+
+ + +
+

What is JSON?

+
    +
  • JavaScript Object Notation
  • +
  • Format for data returned from APIs
  • +
  • You've seen it before!
  • +
  • JavaScript objects
  • +
+
+ + +
+

Getting started!

+

Get an API Key

+

A what?

+
    +
  • Api Key or Developer Key is a way for an API to identify you
  • +
  • More secure for an API (Know who is using their API)
  • +
  • More secure for you -- people can't pretend to be your website
  • +
+
+ + +
+

Meetup API

+

We will be using the Meetup API

+

Documentation

+ +
+ +
+

Meetup API

+

We will be finding interesting Meetups near us

+

Open Events

+ +
+ +
+

Meetup API

+

Try it in the meetup console

+

Open Events Console

+ +
+ +
+

Meetup API

+

Try it in the meetup console

+ +
+ + +
+

jQuery Ajax

+

jQuery method to perform an AJAX call

+
+

+    $.ajax({
+  		url: 'http://site.com',
+  		data: {
+  		  parameter: 'value',
+  		  parameter: 'value'
+  		},
+  		crossDomain: true,
+  		dataType: 'jsonp',
+  		type: "GET",
+  		success: function (data) {
+  		    // code with data returned
+  		},
+  		error: function(data) {
+  			 // code with error returned
+  		}				
+  	});
+          	
+
+
+ +
+

jQuery Ajax

+
+

+    $.ajax({
+  		url: 'http://site.com',
+  		...			
+  	});
+          	
+
+
    +
  • $.ajax() -- jQuery method for sending AJAX requests
  • +
  • Takes one parameter -- a JavaScript object
  • +
  • Note the {}
  • +
  • url -- first property, the url where you will send the AJAX request
  • +
+
+ +
+

jQuery Ajax

+
+

+	...
+	data: {
+	  parameter: 'value',
+	  parameter: 'value'
+	},
+	...	
+          	
+
+
    +
  • data -- a JavaScript object with all the parameters for the AJAX request
  • +
  • Some parameters in the Meetup open events +
      +
    • key (refers to api key)
    • +
    • city
    • +
    • country
    • +
    • topic
    • +
    +
  • +
+
+ +
+

jQuery Ajax

+
+

+	...
+  crossDomain: true,
+	dataType: 'jsonp',
+	type: "GET",
+	...	
+          	
+
+
    +
  • crossDomain -- are you sending the request to a domain that is not your own?
  • +
  • dataType -- how will you evaluate the data returned
  • +
  • type -- what type of request is it? +
      +
    • GET -- retrieve data
    • +
    • POST -- post new data
    • +
    +
  • +
+
+ + +
+

jQuery Ajax

+
+

+	...
+  success: function (data) {
+	    // code with data returned
+	},
+	error: function(data) {
+		 // code with error returned
+	}
+	...	
+          	
+
+
    +
  • success -- code that will execute if results are sent back successfully
  • +
  • error -- code that will execute if results return an error
  • +
+
+ + +
+

Let's Develop It!

+
    +
  • Create a new div with the id "events"
  • +
  • Create a new function in your javascript that calls Meetup open events method
  • +
  • Add parameters such as city, state or lat, lon to find meetups near you
  • +
  • Add a parameter such as category or topic to find meetups that you would be interested in
  • +
  • For now, just look at the results in the console.log()
  • +
  • Call this new function in document ready
  • +
+
+ +
+

Let's Develop It!

+ +
+ +
+

Let's Develop It!

+
    +
  • Create a new function that can parse results.
  • +
  • Remember that the results will be an array of objects
  • +
  • Loop through your results
  • +
  • For each result, create a new div
  • +
  • Get the name, description, group name, and url of each event
  • +
  • Append them to your new div
  • +
  • Append the new div to the div "events"
  • +
+
+ +
+

Let's Develop It!

+
    +
  • Create a form for users to enter in their location and a topic of their choosing
  • +
  • On submit, call the same Meetup open events method, but with the user's values
  • +
+
+ +
+

Bonus

+

Still have time? Can't stop learning?

+
+

One of the best things about jQuery is the developer community

+

They love to build!

+

Check out some great plugins:

+ +
+
+ +
+

Questions?

+
? +
+
+
+ +
+ + + + + + + + + diff --git a/class4/exercise1/index.html b/class4/exercise1/index.html new file mode 100644 index 0000000..f0c627e --- /dev/null +++ b/class4/exercise1/index.html @@ -0,0 +1,40 @@ + + + My Site! + + + + + + My Site!
+
+
+
+
+
+ Events I might like: +
+
+
+
+
+ +
+
+ +
+
+

My favorite things:

+
+
+
+ +
+ +
+
+
+ +
+ + \ No newline at end of file diff --git a/class4/exercise1/javascript.js b/class4/exercise1/javascript.js new file mode 100644 index 0000000..504ece7 --- /dev/null +++ b/class4/exercise1/javascript.js @@ -0,0 +1,85 @@ +$(document).ready(function(){ + getMeetups(); + $('.box').bind({ + click: function() { + $(this).css('background-color', 'green') + $(this).html('Clicked!') + }, + mouseenter: function() { + $(this).css('background-color', 'purple') + $(this).html('Hi!') + }, + mouseleave: function(){ + $(this).css('background-color', 'orange') + $(this).html('Bye!') + } + }); + $('#calculate').submit(function(event){ + var givenAge = $('#age').val(); + var givenSnack = $('#snack').val(); + var givenPerDay = $('#times-per-day').val(); + $('#lifetime-supply').html(calculate(givenAge, givenSnack, givenPerDay)); + return false; + }); + $('#favorites').submit(function(event){ + var givenThing = $('#thing').val(); + favoriteThings(givenThing); + return false + }); + $('#friends').submit(function(event){ + var name = $('#friend-name').val(); + var hair = $('#friend-hair').val() + var friend = {name: name, hair:hair}; + myFriends(friend); + return false; + }); +}); + +function getMeetups(){ + var api_key = "50722e1d56c194e61763a2ee1e4535"; + var url = "https://api.meetup.com/2/"; + var method = "open_events" + $.ajax({ + url: url + method, + data: { + key: api_key, + lat: 40.7143528, + lon: -74.0059731, + topic: 'JavaScript' + }, + crossDomain: true, + dataType: 'jsonp', + type: "GET", + success: function (data) { + console.log(data) + }, + error: function(data) { + console.log("Error", data); + } + }); +} + +function calculate(age, snack, perDay){ + var oldAge = 96; + + var days = (oldAge - age) * 356; + var total = perDay * days; + if(total > 40000){ + return "You will need " + total + " of " + snack + " to last you until the ripe old age of " + oldAge + ". Wow! That's a lot!"; + }else{ + return "You will need " + + total + " of " + snack + " to last you until the ripe old age of " + oldAge + ". You seem pretty reasonable"; + } +} + +function favoriteThings(thing){ + $('#favorite-things').append('

'+ thing +'

'); +} +function myFriends(friend){ + var resultDiv = $('
') + var resultParagraph = $('

' + describeFriend(friend) + '

'); + resultDiv.append(resultParagraph); + $('body').append(resultDiv); +} +function describeFriend(friend){ + return "My friend " + friend.name + " has " + friend.hair + " hair. "; +} \ No newline at end of file diff --git a/class4/exercise1/style.css b/class4/exercise1/style.css new file mode 100644 index 0000000..e182bd6 --- /dev/null +++ b/class4/exercise1/style.css @@ -0,0 +1,17 @@ +.box{ + width: 200px; + height: 100px; + border: 1px solid #ccc; +} + +input{ + width: 200px; + padding: 5px; + margin: 5px; +} +.event{ + margin:5px; + padding:5px; + border: 1px solid #ccc; + background-color: #ddd; +} \ No newline at end of file diff --git a/class4/exercise2/index.html b/class4/exercise2/index.html new file mode 100644 index 0000000..f0c627e --- /dev/null +++ b/class4/exercise2/index.html @@ -0,0 +1,40 @@ + + + My Site! + + + + + + My Site!
+
+
+
+
+
+ Events I might like: +
+
+
+
+
+ +
+
+ +
+
+

My favorite things:

+
+
+
+ +
+ +
+
+
+ +
+ + \ No newline at end of file diff --git a/class4/exercise2/javascript.js b/class4/exercise2/javascript.js new file mode 100644 index 0000000..fe52d41 --- /dev/null +++ b/class4/exercise2/javascript.js @@ -0,0 +1,97 @@ +$(document).ready(function(){ + getMeetups(); + $('.box').bind({ + click: function() { + $(this).css('background-color', 'green') + $(this).html('Clicked!') + }, + mouseenter: function() { + $(this).css('background-color', 'purple') + $(this).html('Hi!') + }, + mouseleave: function(){ + $(this).css('background-color', 'orange') + $(this).html('Bye!') + } + }); + $('#calculate').submit(function(event){ + var givenAge = $('#age').val(); + var givenSnack = $('#snack').val(); + var givenPerDay = $('#times-per-day').val(); + $('#lifetime-supply').html(calculate(givenAge, givenSnack, givenPerDay)); + return false; + }); + $('#favorites').submit(function(event){ + var givenThing = $('#thing').val(); + favoriteThings(givenThing); + return false + }); + $('#friends').submit(function(event){ + var name = $('#friend-name').val(); + var hair = $('#friend-hair').val() + var friend = {name: name, hair:hair}; + myFriends(friend); + return false; + }); +}); + +function getMeetups(){ + var api_key = "50722e1d56c194e61763a2ee1e4535"; + var url = "https://api.meetup.com/2/"; + var method = "open_events" + $.ajax({ + url: url + method, + data: { + key: api_key, + lat: 40.7143528, + lon: -74.0059731, + topic: 'JavaScript' + }, + crossDomain: true, + dataType: 'jsonp', + type: "GET", + success: function (data) { + parseMeetups(data.results) + }, + error: function(data) { + console.log("Error", data); + } + }); +} + +function parseMeetups(results){ + for(var i = 0; i < results.length; i ++){ + var div = $('
'); + var name = $('
Name: '+ results[i].name+'
'); + var description = $('
Description: '+ results[i].description+'
'); + var group = $('
Group: '+ results[i].group.name+'
'); + var link = $('
Learn More: '+results[i].event_url+'
') + div.append(name, description, group, link); + $('#events').append(div); + } +} + +function calculate(age, snack, perDay){ + var oldAge = 96; + + var days = (oldAge - age) * 356; + var total = perDay * days; + if(total > 40000){ + return "You will need " + total + " of " + snack + " to last you until the ripe old age of " + oldAge + ". Wow! That's a lot!"; + }else{ + return "You will need " + + total + " of " + snack + " to last you until the ripe old age of " + oldAge + ". You seem pretty reasonable"; + } +} + +function favoriteThings(thing){ + $('#favorite-things').append('

'+ thing +'

'); +} +function myFriends(friend){ + var resultDiv = $('
') + var resultParagraph = $('

' + describeFriend(friend) + '

'); + resultDiv.append(resultParagraph); + $('body').append(resultDiv); +} +function describeFriend(friend){ + return "My friend " + friend.name + " has " + friend.hair + " hair. "; +} \ No newline at end of file diff --git a/class4/exercise2/style.css b/class4/exercise2/style.css new file mode 100644 index 0000000..e182bd6 --- /dev/null +++ b/class4/exercise2/style.css @@ -0,0 +1,17 @@ +.box{ + width: 200px; + height: 100px; + border: 1px solid #ccc; +} + +input{ + width: 200px; + padding: 5px; + margin: 5px; +} +.event{ + margin:5px; + padding:5px; + border: 1px solid #ccc; + background-color: #ddd; +} \ No newline at end of file diff --git a/class4/exercise3/index.html b/class4/exercise3/index.html new file mode 100644 index 0000000..c61e67d --- /dev/null +++ b/class4/exercise3/index.html @@ -0,0 +1,45 @@ + + + My Site! + + + + + + My Site!
+
+
+
+
+
+
+
+ +
+
+ Events you might like: +
+
+
+
+
+ +
+
+ +
+
+

My favorite things:

+
+
+
+ +
+ +
+
+
+ +
+ + \ No newline at end of file diff --git a/class4/exercise3/javascript.js b/class4/exercise3/javascript.js new file mode 100644 index 0000000..0203e36 --- /dev/null +++ b/class4/exercise3/javascript.js @@ -0,0 +1,101 @@ +$(document).ready(function(){ + $('.box').bind({ + click: function() { + $(this).css('background-color', 'green') + $(this).html('Clicked!') + }, + mouseenter: function() { + $(this).css('background-color', 'purple') + $(this).html('Hi!') + }, + mouseleave: function(){ + $(this).css('background-color', 'orange') + $(this).html('Bye!') + } + }); + $('#calculate').submit(function(event){ + var givenAge = $('#age').val(); + var givenSnack = $('#snack').val(); + var givenPerDay = $('#times-per-day').val(); + $('#lifetime-supply').html(calculate(givenAge, givenSnack, givenPerDay)); + return false; + }); + $('#favorites').submit(function(event){ + var givenThing = $('#thing').val(); + favoriteThings(givenThing); + return false + }); + $('#friends').submit(function(event){ + var name = $('#friend-name').val(); + var hair = $('#friend-hair').val() + var friend = {name: name, hair:hair}; + myFriends(friend); + return false; + }); + + $('#meetup').submit(function(event){ + + getMeetups($('#topic').val(), $('#zipcode').val()) + return false; + }); +}); + +function getMeetups(topic, zipcode){ + var api_key = "50722e1d56c194e61763a2ee1e4535"; + var url = "https://api.meetup.com/2/"; + var method = "open_events" + $.ajax({ + url: url + method, + data: { + key: api_key, + zip: zipcode, + topic: topic + }, + crossDomain: true, + dataType: 'jsonp', + type: "GET", + success: function (data) { + parseMeetups(data.results) + }, + error: function(data) { + console.log("Error", data); + } + }); +} + +function parseMeetups(results){ + for(var i = 0; i < results.length; i ++){ + var div = $('
'); + var name = $('
Name: '+ results[i].name+'
'); + var description = $('
Description: '+ results[i].description+'
'); + var group = $('
Group: '+ results[i].group.name+'
'); + var link = $('
Learn More: '+results[i].event_url+'
') + div.append(name, description, group, link); + $('#events').append(div); + } +} + +function calculate(age, snack, perDay){ + var oldAge = 96; + + var days = (oldAge - age) * 356; + var total = perDay * days; + if(total > 40000){ + return "You will need " + total + " of " + snack + " to last you until the ripe old age of " + oldAge + ". Wow! That's a lot!"; + }else{ + return "You will need " + + total + " of " + snack + " to last you until the ripe old age of " + oldAge + ". You seem pretty reasonable"; + } +} + +function favoriteThings(thing){ + $('#favorite-things').append('

'+ thing +'

'); +} +function myFriends(friend){ + var resultDiv = $('
') + var resultParagraph = $('

' + describeFriend(friend) + '

'); + resultDiv.append(resultParagraph); + $('body').append(resultDiv); +} +function describeFriend(friend){ + return "My friend " + friend.name + " has " + friend.hair + " hair. "; +} \ No newline at end of file diff --git a/class4/exercise3/style.css b/class4/exercise3/style.css new file mode 100644 index 0000000..e182bd6 --- /dev/null +++ b/class4/exercise3/style.css @@ -0,0 +1,17 @@ +.box{ + width: 200px; + height: 100px; + border: 1px solid #ccc; +} + +input{ + width: 200px; + padding: 5px; + margin: 5px; +} +.event{ + margin:5px; + padding:5px; + border: 1px solid #ccc; + background-color: #ddd; +} \ No newline at end of file diff --git a/images/console.png b/images/console.png new file mode 100644 index 0000000..ee941c8 Binary files /dev/null and b/images/console.png differ diff --git a/images/foursquare.png b/images/foursquare.png new file mode 100644 index 0000000..776a4cf Binary files /dev/null and b/images/foursquare.png differ diff --git a/images/meetup.png b/images/meetup.png new file mode 100644 index 0000000..7af8cf8 Binary files /dev/null and b/images/meetup.png differ diff --git a/images/meetup_console.png b/images/meetup_console.png new file mode 100644 index 0000000..7fb5a3a Binary files /dev/null and b/images/meetup_console.png differ diff --git a/images/meetup_open.png b/images/meetup_open.png new file mode 100644 index 0000000..fe09581 Binary files /dev/null and b/images/meetup_open.png differ diff --git a/images/meetup_results.png b/images/meetup_results.png new file mode 100644 index 0000000..c692a09 Binary files /dev/null and b/images/meetup_results.png differ diff --git a/images/results.png b/images/results.png new file mode 100644 index 0000000..94aea09 Binary files /dev/null and b/images/results.png differ