News for hackers. Stuff that matters.
Re:Andro.js by swannodette on Tuesday February 21, 2012 @11:22AM (#3616989)
Nice post! It's interesting that hardly anyone has chimed in that some programming language features make addressing this problem considerably simpler without forcing the programmer to write excess boilerplate. Objective-C delegation, JavaScript Proxies https://developer.mozilla.org/en/JavaScript/Reference/Global..., ClojureScript protocols all provide the machinery to make this kind of thing work without making the code too hard to reason about.
Re:Andro.js by david_a_r_kemp on Tuesday February 21, 2012 @09:47AM (#3616597)
I really like the loosely coupled approach.

However, I think that sprinkling andro.eventer all over the place is a stumbling block to me - it's essentially a global variable.

I realise that you don't want to polute the objects too much, but why not just add bind and emit methods to the composed object? Either that or pass andro as a parameter to the mixin's setup method?

Also, the bind(this, "touch", function...) to me looks like you're binding to the "touch" event of "this". Is the closure tax really so high that you can't store "this", or use something like jQuery.proxy()?

Like I said, I think this approach has a lot of potential, and seems to fit quite naturally the eventful model that the DOM uses.

Re:Andro.js by maryrosecook on Tuesday February 21, 2012 @10:23AM (#3616742)
Thank you for your excellent feedback.

Yes, andro.eventer is there to avoid polluting the owner object. However, your idea of passing andro to the behaviour's setup() method is a great one. I'm going to give it a try.

The reason I require the behaviour to be passed to bind() is so that the passed function can be run with `this` set to the behaviour. I couldn't see another way to let bind know which behaviour should be bound to the event.

Re:Andro.js by jcromartie on Tuesday February 21, 2012 @10:02AM (#3616659)
I wish Node.js had not introduced "evented" as an adjective, leading to "event" as a verb and "eventer" as a noun...
Re:Andro.js by revelation on Tuesday February 21, 2012 @08:37AM (#3616374)
string hell squared.
Re:Andro.js by maryrosecook on Tuesday February 21, 2012 @08:41AM (#3616383)
I'm very open to critique. Can you elaborate?
Re:Andro.js by caller9 on Tuesday February 21, 2012 @09:12AM (#3616462)
I cant speak for that guy but my take is that all of the strings except rarrr should statically defined in the object that defines their behaviour. They may even be integers instead of strings to speed things up. Then you would need some way to avoid collision for the event integers shared across many objects.

Repeating strings is hard to refactor and comparing strings is much less efficient than comparing integers. Think about making sure every bit in two 2 * 8 * length string of bits plus overhead is the same vs two 32bit integers. The latter far fewer cycles.

Re:Andro.js by pavlov on Tuesday February 21, 2012 @09:24AM (#3616509)
I'm not convinced this is useful JavaScript optimization advice. JavaScript doesn't have integers, and string literals are interned, so it shouldn't make a substantial difference if you have the same literal written out 15 times or just defined once and placed in a property. (In fact, using the plain literal everywhere may be faster because it omits the dynamic property access.)
Re:Andro.js by jcromartie on Tuesday February 21, 2012 @10:04AM (#3616667)
I'd rather mistype an identifier than a string literal, because nothing will complain about the wrong string literal.
Re:Andro.js by maryrosecook on Tuesday February 21, 2012 @09:21AM (#3616493)
Excellent point. I would normally do this, but I wanted to reduce the amount of indirection in the code as much as possible. But, it may still be worth doing so people aren't distracted by thinking "Wow, this girl should use constants".
Re:Andro.js by brunoc on Tuesday February 21, 2012 @09:30AM (#3616532)
Strings can work fine for event names, I think op is overstating the string hell factor. But say you wanted to eliminate them you could do something like:

  var firstTouchBehaviour = {
    touchCount: 0,
    events: {
      firstTouch: 'FirstTouch'
    }, 
    setup: function(owner) {
      andro.eventer(owner).bind(this, "touch", function(contact) {
        if(contact === "added") {
          if(this.touchCount === 0) {
            andro.eventer(owner).emit(this.events.firstTouch);
          }
          this.touchCount++;
        } else if(contact === "removed") {
          this.touchCount--;
        }
      });
    }
  };
and

  var soundBehaviour = {
    setup: function(owner) {
      andro.eventer(owner).bind(this, firstTouchBehaviour.events.firstTouch , function() {
        console.log("Rarrrrrwwwwwwwwwwwwwwww");
      });
    }
  };
Re:Andro.js by maryrosecook on Tuesday February 21, 2012 @09:35AM (#3616553)
Yes, I agree.
Re:Andro.js by peter_l_downs on Tuesday February 21, 2012 @08:40AM (#3616381)
What would you do instead?