I’m a big fan of CoffeeScript. It makes working with Backbone.js so much nicer than doing it in VanillaJS.

However, one thing I wish CS had was built in support for mixin declarations. There was some discussion on a closed pull request for adding support for mixins, but no progress was made.

One of the snippets from the pull request gave me an idea for faking mixin support.

The following code will add an include function to the Backbone objects.

include = (mixins...) ->
    throw('include(mixins...) requires at least one mixin') unless mixins and mixins.length > 0

    for mixin in mixins
      for own key, value of mixin
        @::[key] = value

      included = mixin.included
      included.apply(this) if included
    @

  Backbone.Model.include = Backbone.Collection.include = Backbone.View.include = Backbone.Router.include = include
                      

I use it with RequireJS and the facade pattern, so all my modules will load Backbone from the facade, thus everything is setup nicely. :)



blog comments powered by Disqus