Ready to get started?
Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.
A Deep Dive Into Mixins & Compose
In this lesson, we'll walk through what mixins are and how they work by first inspecting them as plain objects then working our way into classes then finally TypeScript. We'll then implement mixins via AdonisJS' compose utility.
- Author
- Tom Gobich
- Published
- Dec 17, 23
- Duration
- 11m 21s
Developer, dog lover, and burrito eater. Currently teaching AdonisJS, a fully featured NodeJS framework, and running Adocasts where I post new lessons weekly. Professionally, I work with JavaScript, .Net C#, and SQL Server.
Adocasts
Burlington, KY
Ready to get started?
Join Adocasts Plus for $8.00/mo, or sign into your account to get access to all of our lessons.
Join The Discussion! (6 Comments)
Please sign in or sign up for free to join in on the dicussion.
ArthurFranckPat
Thank you for the video. I have few questions : Can we use decorators inside the mixin ? For exemple if we want to define a relationship between the model and itself (a recursive relationship)
Please sign in or sign up for free to reply
tomgobich
Hey Arthur! I just gave it a go and everything seems to be working a-okay with using decorators in a mixin! Only caveat is, it looks like you have to give the class a name and return it via that name on a separate line for TypeScript to be happy. Below is what I tried, note the imports are for AdonisJS 6, but the functionality would be the same between 5 & 6.
As a heads up, you can define a parent/child relationship directly on the model without using compose. It'll look like the below.
Hope this helps!
—-
Edit: Updated to account for the TypeScript requirement of the mixin class needing a name and for the return to be on a separate line. I missed the small red squiggly on the decorator when I first attempted this.
Please sign in or sign up for free to reply
danilesky17
Hello, what about this in adonisJS v6 , there is no such a Normalizing class or maybe i cannot find it in documentation ? Thanks for answear :)
Please sign in or sign up for free to reply
tomgobich
Hi Danilesky17! Hmm, yeah it seems to have slipped from the documentation. Luckily though, it is still within AdonisJS, it's just now under the core helpers. Below is the example from this lesson updated for AdonisJS 6!
Hope this helps!
Please sign in or sign up for free to reply
danilesky17
Tom, thank you for quick response! It helped me a lot. I couldn't find this constructor before and VS code didn't offer me when i was trying to find it. Now it seems to be working correctly. Thanks.
Please sign in or sign up for free to reply
tomgobich
Anytime!! Yeah, I tried the same thing and had the same result. It's a hidden little bugger lol 😊
Please sign in or sign up for free to reply