Today we investigate the wonders of (click) events and catching them in our backing component. We’ll be tweeted and favoriting up a storm.

You can always grab tagged source code on the GitHub repo which will be tagged day1, day2, etc.

You can Subscribe to my Fresh Bytecode channel for regular Java/Web-related screencasts, or if you’re just into Angular, checkout the YouTube Playlist where I update all the episodes in this series as they come out.

Here’s the script to follow along at home:

[ ] First we’re going to implement our Favorite and Retweet buttons using (click) Events

(click)='OnFavorite(tweet)'

(click)='OnRetweet(tweet)'

[ ] Implement the click handlers too

OnFavorite(tweet) {
    tweet.favorites.push('Glen');
}

OnRetweet(tweet) {
    tweet.retweets.push('Glen');
}

[ ] Stop double-adds? Be great to do it “properly” with a Tweet object method

isUserInCollection(collection : string[], userId : string) : boolean {

    return collection.indexOf(userId) != -1;

}

OnFavorite(tweet) {
    if (!this.isUserInCollection(tweet.favorites, 'Glen')) {
        tweet.favorites.push('Glen');
    }
}

OnRetweet(tweet) {
    if (!this.isUserInCollection(tweet.retweets, 'Glen')) {
        tweet.retweets.push('Glen');
    }
}

[ ] Demonstrate only adding yourself once