Questions tagged [mixins]

A mixin is a way to enhance an object with properties or methods from another object without inheriting from that object.

Mixins are related to inheritance in that an object receives properties or methods from another object but are typically not limited in number. They are often used as a way to "tack on" behavior to objects rather then to say that objects are related to each other.

For example, we may have an Album class. Since an album has multiple tracks, we may want to enumerate through it; however, it is also very similar to our Single class. In our design, we may then inherit from Single, but also choose to mix in Enumerable, a mixin that defines mechanisms for enumerating an object.

2137 questions
18
votes
3 answers

mixin vs plugin. What to choose?

Let's assume I have defined several new components -MyComponent1, MyComponent2, ..., which extend Ext.Component. Now I wnat to extend all this widgets with the same functionality - I want to add close button which would appear at the top-right…
Molecular Man
  • 22,277
  • 3
  • 72
  • 89
18
votes
1 answer

Type-safe mixin decorator in TypeScript

I tried to define type-safe mixin() decorator function like follows, type Constructor = new(...args: any[]) => T; function mixin(MixIn: Constructor) { return function decorator(Base: Constructor) : Constructor { …
Kiikurage
  • 195
  • 1
  • 6
18
votes
4 answers

Can I define a LESS mixin to generate a transition-property with a variable number of parameters?

I'm introducing LESS to a large web app project to simplify my CSS. I've got a few CSS rules which apply transitions to a varying number of properties, for example: .movable { transition-property: top, left; transition-duration: 0.2s; …
Mark Whitaker
  • 8,465
  • 8
  • 44
  • 68
17
votes
4 answers

What's the difference between mixin() and extend() in Javascript libraries

I'm looking through various libraries, and seeing extend() pop up a lot, but I'm also seeing mixin() show up. YUI has both mixins and extensions. What's the difference between these two concepts? When would I decide between a mixin and extending an…
Matt
  • 22,224
  • 25
  • 80
  • 116
17
votes
1 answer

LESS mix-in for nth-child?

I'm trying to make a LESS mixin that will give me this output: .resource:nth-child(8n+1) { clear: left; } I've got this so far: .wrap-every(@n) { &:nth-child(@n + "n+1") { // parse error on this line clear: left; } } .resource { …
recursive
  • 83,943
  • 34
  • 151
  • 241
17
votes
3 answers

How to use Compass filter mixin?

I'm trying to use filter mixin this way in SCSS: a { @include filter(grayscale(100%)); } But when I compile, I got this error: Undefined mixin 'filter'. I'm using the latest version of this Gem with Rails…
Caio Tarifa
  • 5,973
  • 11
  • 46
  • 73
16
votes
2 answers

Why are template mixins in C++ not more of a mainstay?

I use template mixins in C++ a lot, but I'm wondering why the technique isn't used more. It seems like the ultimate in reuse. This mix of power and efficiency is one of the reasons I really love C++ and can't see myself moving to a JIT…
Jesse Pepper
  • 3,225
  • 28
  • 48
16
votes
3 answers

How to include a module in a factory_girl factory?

I'm trying to reuse a helper method in all my factories, however I cannot get it to work. Here's my setup: Helper module (in spec/support/test_helpers.rb) module Tests module Helpers # not guaranteed to be unique, useful for generating…
mbillard
  • 38,386
  • 18
  • 74
  • 98
16
votes
3 answers

Adding properties to a class via decorators in TypeScript

On the TypeScript's Decorator reference page there is a code snipped that illustrates how to override the constructor with class decorator: function classDecorator(constructor:T) { return class extends…
Forseti
  • 2,587
  • 6
  • 21
  • 32
16
votes
2 answers

Bootstrap 4 media-queries mixins not working with Bootstrap 4 via CDN

I'm using bootstrap 4 connected via CDN, all works fine, but only mixins not working. I have this error in php storm: "Cannot find mixin 'media-breakpoint-down'. This inspection warns about sass/scss mixins references which can't be resolved to any…
Belial
  • 668
  • 2
  • 8
  • 27
16
votes
4 answers

What are some good examples of Mixins and or Traits?

I was reading up on Ruby, and learned about its mixins pattern, but couldn't think of many useful mixin functionality (because I'm not used to thinking that way most likely). So I was wondering what would be good examples of useful Mixin…
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
16
votes
3 answers

CSS LESS Placeholder Mixin

I want to create a placeholder mixin as follows. However, this fails to compile in LESS version 1.7.0. .placeholder(...) { ::-webkit-input-placeholder: @arguments; :-moz-placeholder: @arguments; ::-moz-placeholder: @arguments; …
Matthew
  • 2,871
  • 5
  • 34
  • 59
16
votes
4 answers

Objective-C category compared to Mixins

Is the concept of the Objective-C categories in anyway similar to the concept of mixins? If so: what are the similarities? In not: what are the differences?
teabot
  • 15,358
  • 11
  • 64
  • 79
16
votes
4 answers

LESS CSS Pass mixin as a parameter to another mixin

Is there any way to pass one mixin or style's declaration to another mixin as an input parameter? Let's take a look at an example with animation keyframes. Following is how we define keyframes in pure CSS: @-moz-keyframes some-name { from {…
Alex M
  • 1,304
  • 1
  • 14
  • 23
15
votes
5 answers

Django - Mixing ListView and CreateView

I'm want to create one page with a form, and every time I submit the form it adds an item to the list below the form. I can make it work using 2 pages: one page using the mixin CreateView to add items one page ListView to have the list. But I'm…
Nico
  • 292
  • 1
  • 2
  • 9