Basic DOM Rendering with Subscribe

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 6 years ago

While frameworks like Angular 2 and CycleJS provides great ways to update the DOM and handle subscriptions for you, this lesson shows how you can still do basic subscribe blocks and manually update the DOM on your own.

[00:00] Frameworks that can render out streams like Angular2 and Cycle.js will subscribe to these streams for you inside of either the Angular2 template or using dom drivers in Cycle.js. You just write a lot of streams and let them handle the subscriptions for you.

[00:16] Since we're taking a raw approach, we have to write our rendering inside of our subscribe blocks because these are the side effects of the output of our streams. Instead of just logging out the score, let's go ahead and set up a div that says score with a span ID of score so we can grab that here. Say document query selector with an ID of score. We'll set the inner html to a template string, and then just render out that value of x inside of our stream.

[00:48] Now, when I save and I start my game, I'll go one, two, three. You can see I got a score of two. I can try again. One, two, three. Got a score of zero that time. That way we have a fully working game that you could share with anyone.