Bookmarks for 2013-05-07 through 2013-05-22

These are my links for 2013-05-07 through 2013-05-22:

Bookmarks for 2013-04-24 through 2013-05-06

These are my links for 2013-04-24 through 2013-05-06:

Multiple Inheritance (Mixins) with Backbone.js

I hate having deep inheritance trees using subclasses in Javascript when most of the time you just want to tack on a few useful methods to an object depending on how it is used. Multiple inheritance (or mixins) is great for this however Javascript and many frameworks don't have a clear way to do this.

I have been using Backbone a lot lately and frankly I was a little surprised that Backbone directly didn't have a way to deal with this. Fortunately Backbone is built on top of Underscore and it has a nice method _.extend to help add objects onto others.

Below are two approaches that both seem to work just fine whether you use prototype to define a class or just create an object explicitly with a set of methods and properties. Personally I like the second approach better.

// Define a couple of mixins one using prototype and the other explicitly. Both
// are using "this".
Mixin = function() {};
Mixin.prototype.method = function() {
    console.log(this.property);
    console.log(this.secondProperty);
};
Mixin.prototype.mixinProperty = 'bar';

SecondMixin = {
    secondMethod: function() {
        console.log(this.mixinProperty);
    },
    secondProperty: 'foobar'
};

// This approach does the extend at object initialization. I think this is a
// little messy.
Class = Backbone.Model.extend({
    property: null,
    initialize: function() {
        _.extend(this, new Mixin());
        _.extend(this, SecondMixin);
        this.property = 'foo';
        console.log(this.mixinProperty);
        console.log(this.secondProperty);
        this.secondProperty = 'new value';
    }
});

// This approach does the extend at class definition time. A bit cleaner I
// think. Also even though SecondMixin is used twice here _.extend is making a
// copy and therefore the property values are not shared between the two
// classes.
SecondClass = Backbone.Model.extend(_.extend({
    property: null,
    initialize: function() {
        this.property = 'foo';
        console.log(this.mixinProperty);
        console.log(this.secondProperty);
    }
}, new Mixin(), SecondMixin));

(new Class()).method();
(new SecondClass()).method();

jsFiddle of this code is here.

Bookmarks for 2013-02-26 through 2013-04-18

These are my links for 2013-02-26 through 2013-04-18:

Bookmarks for 2013-01-17 through 2013-02-14

These are my links for 2013-01-17 through 2013-02-14:

Bookmarks for 2012-12-13 through 2013-01-14

These are my links for 2012-12-13 through 2013-01-14:

Bookmarks for 2012-11-09 through 2012-12-10

These are my links for 2012-11-09 through 2012-12-10:

Cider Version 0.7 Released

New version of Cider is released. Cleaned up the UI a bit, added few new editor options and fixed some bugs around the collaborative editing. The change log includes:

  • Added option to the editor to force a single new line onto the end of files on save.
  • Added option to the editor to strip trailing whitespace on save.
  • Fixed a bug in the tab stop setting for markup.
  • The editor now remembers custom set editing modes for specific files.
  • Added conversion support for non-standard characters in text files.
  • The text editor now gains focus on file open.
  • Added better UI and massaging around SSH authentication.

Cider Downloads

Bookmarks for 2012-10-08 through 2012-10-16

These are my links for 2012-10-08 through 2012-10-16:

Bookmarks for 2012-09-26 through 2012-10-05

These are my links for 2012-09-26 through 2012-10-05:

...older