News for hackers. Stuff that matters.
Re:Announcing the first release of batman.js by timothya on Wednesday August 24, 2011 @07:41PM (#2923117)
I'd like to see some examples in JavaScript, instead of CoffeeScript. Since at this point only a smaller subset of developers use CoffeeScript, I think it might be overall useful to more people if the provided examples used plain JavaScript (that, and CoffeeScript developers tend to understand JavaScript already, and may already be used to converting one to the other).
Re:Announcing the first release of batman.js by TrevorBurnham on Wednesday August 24, 2011 @08:48PM (#2923293)
In some cases (e.g. "class Box extends Batman.Object"), the corresponding JS would be much more verbose and less immediately grok-able.

I think CoffeeScript is mainstream enough at this point that CoffeeScript-based documentation is a fine thing. Especially since, as they say at the top of the page, Batman.js "is written in CoffeeScript and its API is developed with CoffeeScript in mind."

Re:Announcing the first release of batman.js by MatthewPhillips on Wednesday August 24, 2011 @11:42PM (#2923687)
> In some cases (e.g. "class Box extends Batman.Object"), the corresponding JS would be much more verbose and less immediately grok-able.

True, but that just basically means that using JS with Batman.js is going to be a pain. In backbone.js you also extend their base classes, but you do it in a js-friendly way (var Widget = Backbone.Model.extend).

Not that there is anything wrong with a CS framework, but they should just say that it's a CS framework. The way CS compiles it's class inheritance to JS is not something I'd want to write raw.

Re:Announcing the first release of batman.js by jashkenas on Wednesday August 24, 2011 @11:51PM (#2923700)
For the record, the way that CoffeeScript does inheritance and the way that Backbone's "extend" does inheritance is almost literally exactly the same thing.

Both simply create an empty object to serve as the prototype (calling new without actually running your constructor), set up the prototype chain correctly, and stash a reference so that calling `super` is easier later on.

If "Child" and "Parent" are constructor functions (class objects), then the basic pattern is this:

    var ctor = function(){};
    ctor.prototype = Parent.prototype;
    Child.prototype = new ctor;
    Child.prototype.constructor = Child;
    
Fun.
Re:Announcing the first release of batman.js by MatthewPhillips on Wednesday August 24, 2011 @11:55PM (#2923716)
This is the way JS devs have been hacking inheritance for a long time. That's irrelevant though, we're not comparing CS to Backbone, we're comparing Batman.js to Backbone.

Backbone handles the messy inheritance code for you. Batman.js relies on CS for that, but a JS dev is going to have to write the inheritance code by hand. I posted the JS equivalent of their example in this thread, it's nasty.

Re:Announcing the first release of batman.js by jashkenas on Thursday August 25, 2011 @12:14AM (#2923744)
It is indeed nasty ... and for that extra nastiness, above and beyond the inheritance pattern listed above, you get:

* A subclass that inherits the parent's implementation of the constructor function, unless overridden.

* A named function for your class object, without IE scope leaks.

* A way to call super that's as easy as `super()`.

* Inheritance of the parent's own properties (unfortunately via copying).

* ... which enables helpful metaprogramming within class bodies. For example, Batman's own `event` decorator:

    use: @event (times) ->
      return false unless ...
Re:Announcing the first release of batman.js by nciagra on Wednesday August 24, 2011 @08:14PM (#2923207)
Sure, valid point. It's not something we work on day to day, so I don't have any readymade examples, but I'll definitely look into it. Thanks!
Re:Announcing the first release of batman.js by goodside on Wednesday August 24, 2011 @08:19PM (#2923220)
The subtext is that you should learn CoffeeScript. It's an alternative syntax, not a new language.

They wrote you a nice library and are giving it away under an MIT license. You don't get to pick the style they use to write the examples. That's like going to a science lecture and walking up to the speaker afterwards to say, "Hey, I think you have some great theories, but you really should lose that Australian accent of yours. Most scientists aren't from Australia, you know."

Re:Announcing the first release of batman.js by masterzora on Wednesday August 24, 2011 @08:53PM (#2923309)
They wrote you a nice library and are giving it away under an MIT license. You don't get to pick the style they use to write the examples.

This is a response to something nobody's saying.

Obviously if they write it they get to choose pretty much everything about it. Nobody is disputing such a thing.

However, if one of the goals in releasing this is to be maximally useful, isn't the fact that raw Javascript is more readily understandable to more people a good thing to point out? Especially since it's highly likely that the team actually has the ability to do normal JS just as easily.

If they disagree with such a goal, though, they can feel free to do whatever they want.

Re:Announcing the first release of batman.js by goodside on Wednesday August 24, 2011 @09:01PM (#2923325)
Nobody who knows CoffeeScript needs to be informed that JavaScript is more mainstream.
Re:Announcing the first release of batman.js by masterzora on Wednesday August 24, 2011 @09:15PM (#2923364)
Perhaps not, but that doesn't necessarily imply that the use of CoffeeScript-only examples was a calculated move to reach the audience they wanted instead of whatever came to mind first. It's worth at least reminding them that JS examples might be more useful to more people and letting them decide consciously whether they care.
Re:Announcing the first release of batman.js by MatthewPhillips on Wednesday August 24, 2011 @11:45PM (#2923694)
They use coffeescript's class inheritance. Here's the JS that compiles down to:

http://jashkenas.github.com/coffee-script/#classes

If they showed JS examples it would instantly turn everyone off. Trust me, you do NOT want to use this with JS.

Re:Announcing the first release of batman.js by Cushman on Wednesday August 24, 2011 @11:00PM (#2923595)
This will be maximally useful if you learn CoffeeScript.
Re:Announcing the first release of batman.js by learc83 on Wednesday August 24, 2011 @09:27PM (#2923391)
I'd say it would be closer to the scientist giving the speech language in West Frisian to a group that's composed of 90% English speakers.

Or maybe more accurately a speech in modern English to a group of early modern English speakers.

Re:Announcing the first release of batman.js by mcantelon on Wednesday August 24, 2011 @08:25PM (#2923232)
>It's an alternative syntax

Right... "alternative"... so makes sense to not give examples entirely in it unless your framework is intended solely for Coffeescript users.

Re:Announcing the first release of batman.js by MatthewPhillips on Wednesday August 24, 2011 @11:51PM (#2923701)
Their framework is intended solely for coffeescript devs. Here's their example in javascript:

  (function() {
  var Shopify;
  var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };
  Shopify = (function() {
    function Shopify() {
      Shopify.__super__.constructor.apply(this, arguments);
    }
    __extends(Shopify, Batman.App);
    Shopify.root('products#index');
    Shopify.resources('products');
    return Shopify;
  })();
  Shopify.Product = (function() {
    function Product() {
      Product.__super__.constructor.apply(this, arguments);
    }
    __extends(Product, Batman.Model);
    Product.persist(Batman.RestStorage);
    return Product;
  })();
  Shopify.ProductsController = (function() {
    function ProductsController() {
      ProductsController.__super__.constructor.apply(this, arguments);
    }
    __extends(ProductsController, Batman.Model);
    ProductsController.prototype.index = function() {
      return this.redirect({
        action: 'show',
        id: 1
      });
    };
    ProductsController.prototype.show = function(params) {
      return this.product = Shopify.Product.find(params.id);
    };
    return ProductsController;
  })();
}).call(this);
Re:Announcing the first release of batman.js by nciagra on Thursday August 25, 2011 @12:02AM (#2923725)
To be fair, that's the compiled CoffeeScript. While it certainly is still fairly readable (even writable), the JavaScript you write by hand won't have the same "compiled feel".
Re:Announcing the first release of batman.js by insin on Thursday August 25, 2011 @06:32AM (#2924431)
This is more like what you would write by hand for the above code if the core constructors had a Backbone-style .extend function [1], which it likely would have if normal JavaScript usage was considered for the initial release (which I recognise is what this is - you can't expect everything first time):

    var Shopify = Batman.App.extend()
    Shopify.root('products#index')
    Shopify.resources('products')

    Shopify.Product = Batman.Model.extend()
    Shopify.Product.persist(Batman.RestStorage)

    // I'm never sure about how much I'm expected to mimic CoffeeScript code
    // when using libraries written in CoffeeScript and the source code is
    // written in a language I'm not practiced at reading - I've been bitten in
    // the past by CoffeeScript-only examples which *assume* you're transpiling
    // to JS in your head to insert the implicit return of the result of the
    // last expression.
    Shopify.ProductsController = Batman.Controller.extend({
      index: function() {
        return this.redirect({action: 'show', id: 1})
      }

    , show: function(params) {
        return this.product = Shopify.Product.find(params.id)
      }
    })
If there's common boilerplate associated with each constructor, you could also go further and customise the extend method for each one to take care of it if provided with certain configuration details, e.g.:

    var Shopify = Batman.App.extend({
      root: 'products#index'
    , resources: 'products'
    })
It always puzzles me that people look at some of the implementation details CoffeeScript shields programmers from and throw their hands up in the air and admit defeat [2] when we have a language as flexible as JavaScript at our disposal to hide some of the uglier implementation details in APIs we define.

[1] https://github.com/documentcloud/backbone/blob/master/backbo...

[2] http://news.ycombinator.com/item?id=2923694

Re:Announcing the first release of batman.js by MatthewPhillips on Thursday August 25, 2011 @12:11AM (#2923740)
It's readable, but any framework that requires you to recreate inheritance yourself is going to have a hard time selling to a js audience when Backbone works perfectly well (and can be written in cs just as easily).
Re:Announcing the first release of batman.js by mcantelon on Thursday August 25, 2011 @01:10AM (#2923837)
>Their framework is intended solely for coffeescript devs

Would makes sense to say this on the front page of the project. Most Coffeescript projects do.

Re:Announcing the first release of batman.js by jurre on Thursday August 25, 2011 @07:45AM (#2924537)
I'm not trying to be obnoxious or anything but doesn't this just show how much cleaner CoffeeScript is compared to js?