Mixen is a library that has, over the last few months, changed how we write CoffeeScript at HubSpot.

Mixen allows you to mix classes together, even if they all implement the same method, to build a new class that has just the components you need.  It's made our code more testable, more maintainable, and it has made it much easier to develop new bits and pieces which might not be ready for universal consumption.

Play With Mixen Now

At HubSpot we have a Mixin for Backbone.Model to use our API interface library.  If we mix it into a model, it knows how to talk to our APIs:

class ContactModel extends Mixen(APIBinding, Backbone.Model)
  urlRoot: 'contacts/v1/contact'
It's just like if you had made APIBinding extend Backbone.Model, but you define the relationship when you use them, not when you define them.  If we want the model to be cached, we can add the cachingMixin:
Mixen(Cache, APIBinding, Backbone.Model)
Both mixins can have a sync method without either being clobbered. It doesn't just call each copy of the sync method, it uses the super concept built into CoffeeScript (but works with JavaScript too).  This means you can have a mixin which will stop the sync:
Mixen(Validate, Cache, APIBinding, Backbone.Model)

That's it! No BaseModel full of a bunch of junk no one uses, use what you need, and each mixin is just a class.  Read our documentation for more, and send any useful Mixins you make our way!

View Mixen Documentation

Recommended Articles

Join our subscribers

Sign up here and we'll keep you updated on the latest in product, UX, and engineering from HubSpot.

Subscribe to the newsletter