twittler Flashcards
What are the main functions / event-handlers / operations you will need to implement and perform?
- Refresh/Display Tweets (whether all or individual user’s)
- Username Event-Handler: click on username –> view that user’s tweets
- ViewAllTweets Event-Handler: click on ViewAll button –> view all tweets
- SubmitTweet Event-Handler: click on Submit button –> adds message tweet to array
For each tweet - What are we trying to accomplish?
Store the current tweet
var tweet = current_stream[index]
Each individual tweet will have component parts (msg, username, timestamp), each part is worthy of its own DOM node/HTML element…
- Create the DOM node/HTML element to house the content for each part (along with an identifying class). Store each one in a jQuery object
- Fill the DOM node/jQuery object with appropriate content using .text()-method
- Insert the jQuery object into appropriate DOM location using .prependTo() or .appendTo()
DOM: What to do for each tweet?
Store in a jQuery variable
var $tweet =
Create appropriate DOM node (to house the entire tweet and its other component parts - tweet, timetsamp, username)
What to do w/ the TWEET jQuery object?
Fill it, using the text() method, with the tweet’s message
$tweet.text(tweet.message);
-basically filling the tweet-DIV with the message
So have a text-message in a div (have content in the jQuery object)–> What to do with that div?
Insert the tweet-jQuery-object into the main tweet display
$tweet.prependTo($(‘#tweets’));
// content.prependTo(targetContainer)
DOM: What to do for the username?
Store in a jQuery variable
var $tweetuser =
Create the appropriate DOM element w/ class to house the username
What to do w/ the USERNAME jQuery object?
Fill it with the tweet’s username
$tweetuser.text(tweet.user + “: “);
-basically filling the username-SPAN with the username
So have apropriate username text in the $tweetuser object () –> What to do with the object?
Insert into the div that houses the entire tweet
$tweetuser.prependTo($tweet);
// content.prependTo(targetContainer)
DOM: What to do for the timestamp?
Store in a variable
$tweetTimestamp =
Create a DOM element w/ a class to house the timestamp
What to do w/ the TIMESTAMP jQuery object?
Fill it with the timestamp info
$tweetTimestamp.text(“Tweeted “ + $.timeago(tweet.created_at));
-basically filling the timeStamp-DIV with the timestamp info
So have timestamp text in the appropriate DOM node / jQuery object –> What to do with that object?
Insert into (or at the end of) the tweet-object
$tweet_timestamp.appendTo($tweet);
// content.appendTo(targetContainer)
How to make each tweet’s username-DOM-element correspond with the actual user who created the tweet?
Take the [tweetuser (DOM element/jQuery object)] and set its #id to the username.
$tweetuser.attr(‘id’, tweet.user);
*What do you need the user-EH to do, step by step? Upon clicking the user’s name…
- Prevent bubbling
- Return the id of the closest span element, which should be the username.
- Set to a variable currentUser
- Use that variable as the property name of the streams.users object. The property will contain an array of that user’s tweets.
- Set to the already-initiated variable currentStream (which is used by the *initiateTweet *display function)
- Display the viewAll button
- Hide the inputBox and submitButton
- Execute the initiateTweet function to display the tweets (in this case, the user’s tweets only)
How to implement the user-EH?
When clicking on [tweetDisplay DIV - particular SPAN-named-class]…
$(“#tweetDisplay”).on(“click”, “.user”, function(event) {
Prevent the screen from bubbling up
event.preventDefault();
Find & store the id of that SPAN - should be the username
var currentUser = $(this).closest(“span”).attr(“id”);
Set the currentStream to be that user’s stream
currentStream = streams.users[currentUser];
Display the viewAll-Button
$(“#viewAll”).css(“display”, “block”);
Hide the inputBox & submitButton
$(“#userTweet, #submitTweet).hide();
Display the stream of tweets (for current user)
initiateTweets();
});
What do you need the viewAll-EH to do, step by step? Upon clicking the “View All” Button..
- Set the currentStream variable back to streams.home
- Hide the viewAll-button
- Refresh and display the tweets (now set to all tweets) using initiateTweets
- Show the inputBox and submitButton