Skip to content

Commit a4b8f9c

Browse files
author
Matt Swanson
committed
adding weekly noise 2 recap
1 parent 19dd351 commit a4b8f9c

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

Diff for: _posts/2010-11-7-weekly-noise-2-recap.markdown

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
layout: post
3+
title: Weekly Noise 2 Recap - alert("It's working!");
4+
categories:
5+
- blog
6+
---
7+
The past week I have been working on a
8+
[web-based "announcement board"](http://swanson.github.com/blog/2010/10/31/weekly-noise-2-so-its-like-a-screensaver.html)
9+
using a [slew of Javascript libraries](http://swanson.github.com/blog/2010/11/03/sipping-the-nodejs-koolaid.html).
10+
11+
So how did I do? Well I hit all my goals for the week, which were:
12+
13+
- Get a static page up that receives WebSocket updates ✓
14+
- Make a simple Admin page to CRUD announcement items ✓
15+
- Get basic animation going for the background ✓
16+
17+
The static page was simple enough to setup, especially since I have used `socket.io`
18+
before. I added some basic styling to play around with some CSS3 stuff (`border-radius`
19+
and `box-shadow` are pretty slick). I send a payload and an action type over `socket.io`
20+
to the page and then perform the appropriate action -- adding an announcement, editing
21+
the message or removing it all together -- with `jQuery`.
22+
23+
[![](/static/week2-index-thumb.png)](/static/week2-index.png)
24+
25+
Next, I setup an "Edit" page where you can CRUD announcements. I used jQuery
26+
Ajax POSTs to trigger the actions in the database.
27+
28+
Since I am serving the pages with `node.js`, I had to adjust to an asynchronous programming
29+
model. I haven't been able to wrap my head around this concept in the past -- my previous
30+
attempts at using `monocle` and `twisted` always ended with me frustrated and discouraged.
31+
32+
I spent some time trying to debug why I was not able to pull some documents out of `MongoDB`
33+
and pass them to the template. It took me a while to realize that I need to put the
34+
rendering function in the callback for the database query. Otherwise, my application was
35+
happy to go on ahead and render the page with an empty array.
36+
37+
<script src="https://gist.github.com/667310.js?file=gistfile1.js"></script>
38+
39+
Putting the `res.render()` in the callback ensures that the query has finished and the
40+
page will receive the proper data. I imagine this code could still be improved with some
41+
kind of asynchronous `map` function instead of a for-loop, but it works well enough at the moment.
42+
43+
<script src="https://gist.github.com/667308.js?file=gistfile1.js"></script>
44+
45+
I also ran into a problem with binding jQuery `onClick` callbacks. Since all of the CRUD functionality
46+
uses Ajax POSTs, I was constructing the new announcement divs on-the-fly. If you use the
47+
normal `$('#foo').click( ... )` jQuery code, only the DOM elements that are present when that
48+
function is executed actually get bound.
49+
50+
As a result, any new elements that I add (like when I
51+
created a new announcement) would not respond to events. Fortunately, this is easily fixed by using
52+
[`.live()`](http://api.jquery.com/live/), which binds the event handlers to all selector matches,
53+
even those created dynamically.
54+
55+
![/static/week2-edit.png](/static/week2-edit.png)
56+
57+
The last goal was to get a simple animation going in the background using `raphael.js`.
58+
I want to be able to generate a bunch of polygons and then have them animate along a path.
59+
Once the polygon goes offscreen, my plan is to loop it around back to where it started to
60+
give the illusion of an endless stream.
61+
62+
I used `inkscape` to make an SVG path (which is really just a string of coordinates and
63+
commands) and then had a spinning square follow the path. Since the SVG element lives in
64+
the DOM along with the other page elements, I needed to set the `zIndex` so that the
65+
animation would appear underneath the other text and images on the screen.
66+
67+
All together, it was a productive week. Despite hitting a few obstacles, I learned a
68+
few new tricks and made good progress; the source code for the app is on
69+
[Github](https://github.com/swanson/glorified-screensaver) so feel free to take a look.

Diff for: static/week2-edit.png

41.1 KB
Loading

Diff for: static/week2-index-thumb.png

66.4 KB
Loading

Diff for: static/week2-index.png

126 KB
Loading

0 commit comments

Comments
 (0)