Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Basic DOM Rendering with Subscribe

    John LindquistJohn Lindquist

    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.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    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.

    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.

    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.