Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket.io sample #492

Open
atherdon opened this issue May 2, 2018 · 0 comments
Open

socket.io sample #492

atherdon opened this issue May 2, 2018 · 0 comments
Labels

Comments

@atherdon
Copy link
Member

atherdon commented May 2, 2018

Description/Steps to reproduce

socket = io.connect(window.location.hostname)
source = $("#item-template").html()
template = Handlebars.compile(source)
target = $("#existing-items")

addItem = (item) ->
html = template(item)
target.append(html)

updateItem = (item) ->
$("li[data-id="#{item._id}"]").find('input[type=text]').val(item.name)

deleteItem = (item) ->
$("li[data-id="#{item._id}"]").remove()

for item in window.items
addItem(item)

$('#save').live "click", (e) ->
e.preventDefault()
data = {}
input = $('#new-item')
data.name = input.val()
data._id = input.parents('li').attr('data-id')
data.purchased = false
input.val('')
socket.emit 'new item posted', data

$('.input-prepend input[type=text]').live "blur", ->
name = $(this).val()
_id = $(this).parents('li').attr('data-id')
socket.emit 'item edited', {name: name, _id: _id}

$('.add-on input[type=checkbox]').live "change", ->
_id = $(this).parents('li').attr('data-id')
socket.emit 'item deleted', {_id: _id}

Sockets

socket.on 'new item saved', (data)->
addItem(data)

socket.on 'item was edited', (data) ->
updateItem(data)

socket.on 'item was deleted', (data) ->
deleteItem(data)

Expected result

Additional information

@atherdon atherdon added the links label Jun 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant