Mixins not documented at https://guides.emberjs.com

Does anyone know why mixins is not really covered at https://guides.emberjs.com?

Are they supposed to be used?

I have a class, I want to include a mixin, how do I do it?

1 Like

Some people discourage mixins, we personally use them for things that make sense. Popular addons like ember-simple-auth also use them. To use one in a route for example…

import Ember from 'ember';
import AuthenticatedRouteMixin from "../mixins/authenticated-route-mixin";

export default Ember.Route.extend(AuthenticatedRouteMixin, {
...
});

Why are they discouraged? I must admit again they don’t seem to be documented at that location.

As for why they’re not documented I’m not really sure. As far as why they are discouraged by some people I think it mostly comes down to “they’re easy to abuse”. This thread may shed some more light.

@Ben_Glancy I encourage developers to use component composition instead of inheritance for a few reasons

  1. If you want to use es2015 class syntax in ember, Mixins are not supported so using them will add friction to the migration away from ember object in the near future
  2. Composition helps you avoid shared mutable state and create small stateless functions
  3. When a Mixin alters state on the parent that operation is often less visible to the next developer
  4. Anything you can do with inheritance you can achieve with composition

In years past I wouldn’t get dogmatic about it because I wanted everyone to experience for themselves why (functional) composition produces a better design. But now that es2015 class syntax is becoming more popular in ember … helping teams avoid Mixins will ensure less painful upgrades :slight_smile:

1 Like

If you show a specific use case, maybe we can help show the difference. For the authenticated route example given, you could extend the route and create an AuthenticatedRoute with all the same expectations - and keep things more transparent. I remember seeing many mixins in one project - like, 8 mixins all passed in… and that was hard to know what was being included and how it interacted together.

1 Like

Thanks for that toranb

Yes 8 mixed in together, bad. I can see your point… it means they are not very scalable