-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added in the working API & Handlebars data
- Loading branch information
Showing
4 changed files
with
67 additions
and
30 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,48 @@ | ||
$(document).ready(function(){ | ||
|
||
// Fetch data from the Represent Civic Information API | ||
$.ajax({ | ||
url: "https://represent.opennorth.ca", | ||
dataType: 'jsonp', | ||
success: function(results){ | ||
console.log(results); | ||
} | ||
}); | ||
// Receive form information from user on submit | ||
$('form').on('submit', function(event) { | ||
event.preventDefault(); | ||
|
||
// Set up a Handlebars template for the Representative Contact Card | ||
var source = document.getElementById("representative-card").innerHTML; | ||
var template = Handlebars.compile(source); | ||
|
||
// Capture postal code for API lookup | ||
var postal_code = $('form').find('#postal_code').val(); | ||
|
||
// Do functions with it | ||
// Fetch data from the Represent Civic Information API | ||
$.ajax({ | ||
url: "https://represent.opennorth.ca/postcodes/" + postal_code, | ||
dataType: 'jsonp', | ||
success: function(results){ | ||
var representative = {}; | ||
var length = results.representatives_centroid.length; | ||
var template_data = []; | ||
|
||
console.log('Compiled'); | ||
// For every postal code, there is an array of representative data | ||
// We are looping to find the House of Commons (MP) dataset. | ||
// This is a brittle, quick implementation and could be further | ||
// improved by catching missing rep data or API errors with fallbacks. | ||
for(i=0;i<length;i++) { | ||
representative = results.representatives_centroid[i]; | ||
|
||
if (representative.representative_set_name == "House of Commons") { | ||
template_data = { | ||
full_name: representative.name, | ||
email_address: representative.email, | ||
phone_number: representative.email, // which phone number? | ||
image: representative.photo_url, | ||
riding_location: representative.district_name, | ||
party_affiliation: representative.party_name | ||
}; | ||
break; | ||
} | ||
} | ||
|
||
// Render the Handlebars template with the rep data | ||
$('.js-form-response').html(template(template_data)); | ||
} | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Layout file | ||
|
||
body { | ||
color: #f00; | ||
|
||
} |
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