Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Forked from ryanflorence/events.coffee
Created September 14, 2012 05:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilmoore/3719959 to your computer and use it in GitHub Desktop.
Save wilmoore/3719959 to your computer and use it in GitHub Desktop.
CoffeeScript is clean, but the generated JS is...generated.
@events =
events: {}
on: (topic, handler, context = this) ->
(@events[topic] or= []).push {handler, context}
trigger: (topic, args...) ->
return unless @events[topic]?
handler.apply(context, args) for {handler, context} in @events[topic]
var __slice = [].slice;
this.events = {
events: {},
on: function(topic, handler, context) {
var _base;
if (context == null) {
context = this;
}
return ((_base = this.events)[topic] || (_base[topic] = [])).push({
handler: handler,
context: context
});
},
trigger: function() {
var args, context, handler, topic, _i, _len, _ref, _ref1, _results;
topic = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
if (this.events[topic] == null) {
return;
}
_ref = this.events[topic];
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
_ref1 = _ref[_i], handler = _ref1.handler, context = _ref1.context;
_results.push(handler.apply(context, args));
}
return _results;
}
};
(function() {
var slice = Array.prototype.slice;
this.events = {
events: {},
on: function (topic, handler, context) {
if (context == null) context = this;
this.events[topic] = this.events[topic] || []
this.events[topic].push({ handler: handler, context: context });
},
trigger: function (topic) {
if (this.events[topic] == null) return;
var args = slice.apply(arguments, 1);
for (var i = 0, l = this.events[topic].length, event; i < l; i++) {
event = this.events[topic][i];
event.handler.apply(event.context, args);
}
}
};
}).call(this);
@wilmoore
Copy link
Author

Nobody would purposely write JS to match what was generated. Further, the CS version isn't that much more terse than a cleanly written JS version.

I like the idea behind CoffeeScript, but in reality, the generated code is a mess. Not sure why I was expecting anything more to be honest.

@Mparaiso
Copy link

True , the generated javascript is harder to read, i call it the "babylon" effect , like the tower in the first testament. But it is still readable.However there is no 1 to 1 coffee - js translation so , though the cs is compact , i would not have gone for such an extreme "optimisation".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment