Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialYonatan Medan
3,943 Pointsi don't understand how/why does profileJSON gets the proporties of the json object?
studentProfile.on("end", function(profileJSON){
//show profile
var values = {
avatarUrl:profileJSON.gravatar_url,
username: profileJSON.profile_name,
badges: profileJSON.badges.length,
JavaScriptPoint: profileJSON.points.JavaScript
}
4 Answers
Faddah Wolf
12,811 PointsSamuel Webb & Kevin Korte (and Andrew Chalkley, if you're out there),
i believe everyone one here in this forum has totally missed this question, which has been repeated here several times in this forum, regarding this particular module in @chalkers very good (otherwise) course.
everyone keeps directing people to AJAX, jQuery or other API docs to answer this question about general how JavaScript pulls data from the internet. that is not the question they are asking. they are asking, specifically, in this example in @chalkers module, where this particular data is being pulled from? is it because we're on a treehouse workspace and it automatically pull treehouse API data just by adding a ".../[username]
at the end of the URL?
there seems to be no particular code in the example that indicates a URL or other data source that it's pulling this info from, nor have we been given instruction, thus far in this course, how to pull data when the data source is not on your particular server, but another server or data source some where. what people want to know here is, specifically, empirically, where is this particular data in the JSON object coming from?
please answer as regards this specific code, not general AJAX docs. thank you.
best,
ā faddah portland, oregon, u.s.a.
Faddah Wolf
12,811 Pointshi Kevin Korte,
hi. ok, and thank you for answering that in such excellent, specific detail. so, to simplify, all those Object properties (vars) on profileJSON
ā we are getting those because the treehouse workspace is part of the general treehouse API, and we don't need a URL to fetch data, it's all ready exposed there?
i ask because i've done node.js stuff before and usually you have to provide some sort of data location with a URL or whatever as to where you're getting the data from. it would be nice, Andrew Chalkley, if this module showed how to do the same thing fetching from a foreign data source that was not on the same server. just a suggestion.
i also see now that profile.js
has the URL for treehouse i was looking for that is pulling the data, and that we use as the constructor for the new studentProfile
Object instance. that is what was not very clear before in this module ā where exactly in the code was it being instructed to pull from the treehouse URL API. now i understand much better. thank you.
also, if you could make the above ^^^^^ you did into an answer, not a comment on my answer, then it could be marked as the answer to this one and we could put it to bed. thank you again, Kevin, for your very specific, complete answer.
best,
ā faddah
portland, oregon, u.s.a.
Kevin Korte
28,149 PointsHere is how you'd find your answer, looking at jquery's docs, for the .on
method, the last arguement we can pass is the handler. We do get the eventObject
, but optionally we can pass in anything else we want.
handler Type: Function( Event eventObject [, Anything extraParameter ] [, ... ] ) A function to execute when the event is triggered. The value false is also allowed as a shorthand for a function that simply does return false.
In this case we are ignoring the eventObject
and passing in our JSON that gets processed in the callback.
Samuel Webb
25,370 PointsYou're passing in your JSON object as an argument to the function that you're passing to the .on()
method. It's being passed in under the variable named profileJSON
which is then being used inside of the function to set the values in the object named values
.
Samuel Webb
25,370 PointsprofileJSON
is just a variable name which, inside of the function, holds the same value as whatever JSON object you pass into it.
Yonatan Medan
3,943 Pointsbut ist the variable that is passed to the .on() method is alway the event object? like this?
.on("event", function(eventObject){
//do something
}) ;
insted it's like this
.on("event", function (jsonPassedToTheFunction) {
//do something
});
Kevin Korte
28,149 PointsKevin Korte
28,149 PointsOkay, lets reverse engineer it.
profileJSON.gravatar_url
that we expect to have the url of the users gravatar.profileJSON
is the name of the argument we passed into the annoymous function as our callback when we calledstudentProfile.on
.profileJSON
could be anything, as long as it's consistent from the argument to the function, as well as being used inside the anonymous function.studentProfile
knows which user we are talking about, because it is a new instance of the Profile class with the argument as a variableusername
passed in.username
came from the user function, set to handle the HTTP route GET /:username. When you hit the correct, url, we are taking the request argument, and settingusername
to equal therequest.url
, and stripping out the forward slash so what we are left with is just the username, of the user, from the url.new Profile(username)
. Well we know where the username, came from, and so we pass in the username from the request, to create a new Profile, from the profile.js file where, in that file, you can see we make ahttp.get
request tohttp://teamtreehouse.com/ + username + .json
And that is the basis of how, and where, following the chain up, where this web app is making a request to the treehouse server and getting json back