O'Reilly logo

CoffeeScript Programming with jQuery, Rails, and Node.js by Michael Erasmus

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding to-do items to a shared list

First, we'll handle adding new items on the server. The best place to handle this would be in the existing create function for creating to-do items. Instead of just adding them to localStorage, we'll also emit a message to the server telling it that a new to-do item has been created, and pass it as a parameter. Modify the create function to look like the following code:

create: (e) ->
    val = $.trim @$input.val()
    return unless e.which == 13 and val

    randomId = Math.floor Math.random()*999999

    newItem =
       id: randomId
       title: val
       completed: false

    localStorage.setObj randomId, newItem
    @socket.emit 'newItem', newItem if @socket
    @$input.val ''
    @displayItems()

We need to handle the newItem message on the server. We'll ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required