diff options
| author | Kyle K <kylek389@gmail.com> | 2012-07-02 22:26:00 -0500 | 
|---|---|---|
| committer | Kamil Kaminski <kyle@kkaminsk.com> | 2012-07-02 22:26:00 -0500 | 
| commit | 0b2942ec56f8015c41a82937a07680028060a0e7 (patch) | |
| tree | f26932c8d2dcc840492d68f658550d93b158dd05 | |
| parent | 46230d8dd0709829859dc1feb92db17933000fcc (diff) | |
| download | fubar-0b2942ec56f8015c41a82937a07680028060a0e7.tar.gz fubar-0b2942ec56f8015c41a82937a07680028060a0e7.tar.bz2 fubar-0b2942ec56f8015c41a82937a07680028060a0e7.zip | |
learn on events, tweak html
| -rw-r--r-- | events/event_emitter1.js | 13 | ||||
| -rw-r--r-- | events/event_emitter2.js | 33 | ||||
| -rw-r--r-- | notes.txt | 7 | ||||
| -rw-r--r-- | protoype/app.js | 10 | ||||
| -rw-r--r-- | protoype/public/stylesheets/style.css | 8 | ||||
| -rw-r--r-- | protoype/router/driver.js | 6 | ||||
| -rw-r--r-- | protoype/views/index.jade | 18 | ||||
| -rw-r--r-- | samples/ref_to_instance.js | 17 | 
8 files changed, 99 insertions, 13 deletions
| diff --git a/events/event_emitter1.js b/events/event_emitter1.js new file mode 100644 index 0000000..4bf9868 --- /dev/null +++ b/events/event_emitter1.js @@ -0,0 +1,13 @@ +var events = require('events'); +var ee = new events.EventEmitter(); + +ee.on('myevent', function(msg) { +    console.log('handling event, got ' + msg); +}); + +function foo() { +    ee.emit('myevent', 'foo'); +} + +setInterval(foo, 1000); + diff --git a/events/event_emitter2.js b/events/event_emitter2.js new file mode 100644 index 0000000..6845f2a --- /dev/null +++ b/events/event_emitter2.js @@ -0,0 +1,33 @@ +var events = require('events'); +var util = require('util'); + +function MyEventEmitter() { +    if (false === (this instanceof MyEventEmitter)) +        return new MyEventEmitter(); + +    /* inherit all prototype objects, methods, etc */ +    events.EventEmitter.call(this); /* hmm this calls a ctor? notice it's not a method */ +} +/* ensure that the prototype methods of the specified superCtor are inherited into ctor */ +util.inherits(MyEventEmitter, events.EventEmitter); + +MyEventEmitter.prototype.poke = function(msg) { +    this.emit('poke', 'poking ' + msg) +} + +MyEventEmitter.prototype.yell = function(msg) { +    this.emit('poke', 'yelling at ' + msg) +} + +var ee = new MyEventEmitter; + +ee.on('poke', function(msg) { +    console.log(msg); +}) + .on('yell', function(msg) { +    console.log(msg); +}); + +ee.poke('kyle'); +ee.yell('bro'); + @@ -11,3 +11,10 @@ $ jslint --sloppy --white <file.js>  var foo = [1, 2, 3];  var bar = foo.slice(0); +- an object that listen for event must be subclass of +  of events.EventEmitter, e.g. http.createServer() + +  I'm an eventer, I will emit events, and have a list of +  who responds/listens to those, so we will see statements +  such eventer.on('foo', somehandler) + diff --git a/protoype/app.js b/protoype/app.js index 6722eab..2b6828c 100644 --- a/protoype/app.js +++ b/protoype/app.js @@ -32,8 +32,10 @@ app.configure(function() {  });  app.post('/create', function(req, res) { -    db.users.save({tag: req.param('tag'), id: req.param('id'), status: "offline", vehicle: '', -                   avatar: 'images/avatars/default.png', sig: '', location: '', races: 0}, +    db.users.save({tag: req.param('tag'), id: req.param('id'), status: "offline", +    vehicle: {make: req.param('make'), model: req.param('model'), year: req.param('year'), +    desc: req.param('desc')}, userinfo: {sig: req.param('sig')}, location: {loc: req.param('loc')}, +    stats: {matches: 0, won: 0, lost: 0}},      function(err, thing) {          if (err || !thing)              util.log('[create] error saving'); @@ -67,6 +69,9 @@ app.post('/login', function(req, res) {      });  }); +/* /sys/do?get=activelist [list of active users] + * + */  app.get('/sys/:id([a-z]+)', function(req, res, next) {      /* id contains routing token, req.query is a block/struct, key-val data structure containing GET params       * id should be the main verb, action we want to do */ @@ -81,7 +86,6 @@ app.get('/sys/:id([a-z]+)', function(req, res, next) {                          util.log('[sys] do?get=activelist failed or empty');                      else {                          for (var i = 0; i < result.length; i++) { -                            console.log('name is: ' + result[i].tag);                              data += result[i].tag + '\n';                          }                          res.send(data); diff --git a/protoype/public/stylesheets/style.css b/protoype/public/stylesheets/style.css index c4dfff7..73d1927 100644 --- a/protoype/public/stylesheets/style.css +++ b/protoype/public/stylesheets/style.css @@ -1,9 +1,13 @@  body { -    padding: 50px; -    font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; +    padding: 10px; +    font: 12px "Lucida Grande", Helvetica, Arial, sans-serif;  }  a {      color: #00B7FF;  } +#listentry { +    padding: 5px; +} + diff --git a/protoype/router/driver.js b/protoype/router/driver.js index 2eeadbb..05e1039 100644 --- a/protoype/router/driver.js +++ b/protoype/router/driver.js @@ -9,6 +9,12 @@ function newuser(req, res) {                  '<form method="post" action="/create">\n' +                  '    Tag: <input type="text" name="tag" /><br />\n' +                  '    ID: <input type="text" name="id" /><br />\n' + +                '    Make: <input type="text" name="make" /><br />\n' + +                '    Model: <input type="text" name="model" /><br />\n' + +                '    Year: <input type="text" name="year" /><br />\n' + +                '    Desc: <input type="text" name="desc" /><br />\n' + +                '    Signature: <input type="text" name="sig" /><br />\n' + +                '    Location: <input type="text" name="loc" /><br />\n' +                  '    <input type="submit" value="add" />\n' +                  '</form>\n' +                  '</body>\n' + diff --git a/protoype/views/index.jade b/protoype/views/index.jade index cfbdb80..cd300f5 100644 --- a/protoype/views/index.jade +++ b/protoype/views/index.jade @@ -1,11 +1,13 @@ -h1= title -p Welcome to #{title}. +h1(style='border-bottom: dashed #FF9900; letter-spacing: -2px')= title +p Welcome to #{title}. Find your opponent, and get ready! -h3 User list: -ul -  - each user in users -    li= user.tag +h3 User List +- each user in users +  #listentry <u>#{user.tag}</u>       - if (user.status == 'online') -      span(style='color: green')= user.status +      span(style='color: green; font-weight: bold')= user.status      - else -      span(style='color: red')= user.status +      span(style='color: red; font-weight: bold')= user.status +    | <br />#{user.vehicle.year} #{user.vehicle.make} #{user.vehicle.model} +    | <br />mods: #{user.vehicle.desc}<br /> +    span(style='color: gray; font-style: italic')= user.userinfo.sig  diff --git a/samples/ref_to_instance.js b/samples/ref_to_instance.js new file mode 100644 index 0000000..975756f --- /dev/null +++ b/samples/ref_to_instance.js @@ -0,0 +1,17 @@ +function foo() { +    if ((this instanceof foo) === false) +    { +        console.log('not an instance, hence a reference!'); +        /* returns new object of this class, which will trigger runtime of this +         * function */ +        return new foo(); +    } +    else +        console.log('you have an instance?'); +} + +var ref = foo; +ref.call(ref); + +var inst = new foo(); + | 
