Max Brunsfeld's blog



Max BrunsfeldMax Brunsfeld
[Standup][SF] 5/17 - we approve of the juggernaut
edit Posted by Max Brunsfeld on Thursday May 17, 2012 at 07:27PM

Ask for Help

"Is Juggernaut still the best library to use for websockets, long-polling etc?"

The last commit on master was about 2 months ago. One pivotal project is using it, and they approve of it. If you're on Heroku, you might try their pusher service.

Ask for Help

Interesting Things

  • If you're setting up a postgres database using ActiveRecord, and you see an error that looks like this:

    new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)

then the problem may be that your template1 database (the default database that postgres copies from when you CREATE DATABASE) is configured to use ASCII, rather than UTF8. Try adding this option to the relevant section of your database.yml:

template: template0

More information about Template Databases in postgres

  • If you donate to Travis CI, then you will receive free stickers.

Max BrunsfeldMax Brunsfeld
A convenient 'super' method for Backbone.js
edit Posted by Max Brunsfeld on Monday January 30, 2012 at 07:56PM

Inheritance in Backbone

Backbone.js comes with a minimalist OO inheritance framework similar to the one employed by CoffeeScript. Each base class has a static method called extend that is used to create a subclass, like this:

User = Backbone.Model.extend({
  // instance methods
},
{
  // class methods
});

extend returns a constructor whose prototype inherits from Backbone.Model.prototype. References to all of Backbone.Model's static methods and properties (including extend) are copied to the new constructor.

Calling 'super'

The constructor also receives a __super__ property, which references its superclass.

User.__super__ === Backbone.Model.prototype

This makes it possible to call super inside of a class or and instance method:

User.prototype.save = function(attrs) {
    this.beforeSave(attrs);
    User.__super__.save.apply(this, arguments);
};

CoffeeScript has a super keyword that compiles to the line above, but when using Backbone with plain javascript, its a little grating to have to type that out.

A small layer of convenience

I wrote this little super method (test-driven using jasmine) which saves me having to repeat the constructor's name all over the place. You call it like this:

User.prototype.save = function(attrs) {
    this.beforeSave(attrs);
    this._super("save", arguments);
};

The second parameter to _super is the array of arguments to pass to the overridden method. This is to optimize for the common case of passing the arguments object straight through.

There's no way to avoid having to repeat the method name like that, unless you wrap every method definition with a helper function that either passes the overridden method as a parameter (a la Prototype.js) or reassigns a hidden super property behind the scenes (like JS.Class or John Resig's approach). These approaches won't work with Backbone's ultra-minimalist inheritance system.

Max BrunsfeldMax Brunsfeld
Standup 8/26/2011
edit Posted by Max Brunsfeld on Saturday August 27, 2011 at 01:11AM

Interesting Things

  • Radio buttons behave strangely when they have no 'value' attribute. If you had a form containing a tag like this:

<input type="radio" name="foo" /> (no value attribute),

and you selected that radio button, then the POST data will contain foo=on. The default value is not an empty string, but the word "on".

You might run into this problem using rails form helpers. If you pass a nil value to ActionView's radio_button_tag method, it will render a tag without a 'value' attribute, so the string "on" might show up in your params.

  • Watch out for old middleware that expects the :body method of a Rack::Response to return a string. In newer versions of rack, the method returns an enumerable. We ran into this problem with an old version of pdfkit. It's fixed in the new version.

Max BrunsfeldMax Brunsfeld
Standup 8/24/2011
edit Posted by Max Brunsfeld on Wednesday August 24, 2011 at 09:43PM

Ask for Help

Cocoa applications can be made scriptable with AppleScript. Unfortunately, nobody at the office this morning had experience with this.

Interesting Things

SF Pivots should check out the free PCs in the ops area. These were cutting edge linux development machines several years back, and now they and there parts are up for grabs!

Max BrunsfeldMax Brunsfeld
Standup 8/22/2011
edit Posted by Max Brunsfeld on Monday August 22, 2011 at 09:39PM

Ask for Help

"Is there a Ruby Gem that will take a string of code and identify what programming language it's written in?"

Github has a gem called Linguist, but it doesn't identify the language by parsing it. It uses file extensions, hash bang lines, and looks for the presence of certain keywords in the code.

"Should Akamai be used with, or as a replacement for varnish?"

Akamai is a complete replacement for varnish.