-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheatsheet
56 lines (36 loc) · 1.48 KB
/
Cheatsheet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
STEP 1: render form
<div class="inputs">
<div class="padder">
<form action="/msg_submit" method="post">
<input name="username" type="text" placeholder="Your name" />
<input name="body" type="text" placeholder="Message" />
<input type="submit" value="Send" />
</form>
</div>
</div>
STEP 2: Ajax-send form
$.post($(this).attr("action"), $(this).serialize());
$("input[name=body]").val("").focus();
STEP 3: handle POST
body = self.post.get("body", "-")
username = self.post.get("username", "John Doe")
timestamp = datetime.now().strftime("%H:%M")
STEP 4: send realtime message
message = { "body": body, "username": username, "timestamp": timestamp}
Push.sendToChannel("**global_chat", "ChatMsgReceived", message)
STEP 5: Receive realtime message in Javascript
Push.bind("ChatMsgReceived", function (event) {
var msgElement = $("#chatMsgTmpl").tmpl(event.data);
$(".chat ul").append(msgElement)
$(".chat .scroller").scrollTop($(".chat .scroller ul").outerHeight())
})
STEP 6: Define template
{* jQuery template for chat message *}
<script id="chatMsgTmpl" type="text/x-jquery-tmpl">
<li>
<span class="timestamp">${ timestamp }</span>
<p class="body"><span class="username">${ username }:</span> ${ body }</p>
</li>
</script>
STEP 7: Render template
var msgElement = $("#chatMsgTmpl").tmpl(event.data);