Over the last several months, I’ve been building an ambitious Batman.js and Rails app (it’s going great!). In the course of things, I needed a way to group similar functionality like I would in a concern-based module in Rails. The mixin fits the bill perfectly.
Let’s say you have several Batman models that all have some attributes that share something in common, like maybe they have a common accessor to deal with an excessively terse status code:
1 2 3 4 5 6 7 8 9 | |
Well, if Foo and Bar both need accessors like this, a mixin will enable you to share the code between them.
To start, I create a mixins directory at app/assets/javascripts/batman/mixins and then require it before my models, like so:
1 2 3 4 | |
Then I can create the first mixin like so:
1 2 3 4 5 6 7 8 9 10 | |
And include it in Foo and Bar with:
1 2 | |
Couldn’t be easier to keep things DRY.
Of course, there are many ways to reduce code duplication and this is just one, but hopefully it helps you as much as it did me.