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
28
votes
6 answers

How to set keyframes name in LESS

I try to set up this LESS mixin for CSS animation keyframes: .keyframes(@name, @from, @to) {; @-webkit-keyframes "@name" { from { @from; } to { @to; } } } but there is some problem with name pharse, is there any…
Lukas
  • 7,384
  • 20
  • 72
  • 127
27
votes
4 answers

Are traits not simply composition?

I was reading an article about the new features in PHP 5.4.0. One of the most anticipated one being Traits. Reading up on these Traits, to see what they're all about, they simply look as compiler assisted copy-paste to me; and a language provided…
nkr1pt
  • 4,691
  • 5
  • 35
  • 55
27
votes
4 answers

Getting a list of classes that include a module

I have a mixin for which I would like to get a list of all the classes that have included it. In the mixin module, I did the following: module MyModule def self.included(base) @classes ||= [] @classes << base.name end def…
jangosteve
  • 1,562
  • 2
  • 14
  • 26
27
votes
6 answers

Where to put common code found in multiple models?

I have two models that contain the same method: def foo # do something end Where should I put this? I know common code goes in the lib directory in a Rails app. But if I put it in a new class in lib called 'Foo', and I need to add its…
Bryan Locke
  • 2,337
  • 5
  • 25
  • 30
26
votes
1 answer

Django: Creating a Mixin for Reusable Model Fields

I've got a few fields that I want to add to most every model in my project. For example, these fields are "tracking fields" such as a created date, an update date, and an "active" flag. I'm attempting to create a Mixin that I could add to each…
Joe J
  • 9,985
  • 16
  • 68
  • 100
26
votes
4 answers

Sharing common CSS across VueJS components

I'm working on the VueJS 2 project and I'm trying to clean the code but struggle with scoped styling. Here is my requirements. :) I have 3 components those are very similar to each others, so I decide to use mixins to merge the code into one file.…
spicydog
  • 1,644
  • 1
  • 17
  • 32
26
votes
4 answers

Does Objective-C support Mixin like Ruby?

In Ruby, there's Modules and you can extend a class by "mixing-in" the module. module MyModule def printone print "one" end end class MyClass include MyModule end theOne = MyClass.new theOne.printone >> one In Objective-C, I find that…
hacksignal
  • 633
  • 1
  • 7
  • 11
26
votes
8 answers

Bootstrap 3: adding a new set of columns

I've been using Bootstrap 3 for a while and now I need to make a new set of extra small columns for horizontal mobiles (e.g. 384px screen width) and after this use it as follows: col-xxs-1, col-xxs-2, col-xxs-offset-5, hidden-xxs, etc. Are there…
Facundo Colombier
  • 3,487
  • 1
  • 34
  • 40
25
votes
5 answers

Decorators versus inheritance

How do you decide between using decorators and inheritance when both are possible? E.g., this problem has two solutions. I'm particularly interested in Python.
Neil G
  • 32,138
  • 39
  • 156
  • 257
25
votes
2 answers

LESS css set dynamic background image with mixin

I am using LESS CSS . I am currently using Mixins with variables. Something like this works okay : .border-radius (@radius) { border-radius: @radius; } #header { .border-radius(4px); } This is not : .bg-img(@img) { background-image:url(@img);…
DMin
  • 10,049
  • 10
  • 45
  • 65
25
votes
4 answers

EventEmitter and Subscriber ES6 Syntax with React Native

I am trying to implement an EventEmitter/Subscriber relationship between two components in a react native class. I have seen referenced the following materials: React Native - Event Emitters by Colin Ramsay React Native - Call Function of child…
Adam Jakiela
  • 2,188
  • 7
  • 30
  • 48
24
votes
6 answers

How can I mix in Singleton to create a class that accepts initialization parameters?

I've seen how to define a class as being a singleton (how to create a singleton in Ruby): require 'singleton' class Example include Singleton end But what if I want to pass some parameters to #new when initializing that single instance? Example…
codecraig
  • 3,118
  • 9
  • 48
  • 62
24
votes
1 answer

Python: Use of decorators v/s mixins?

I have understood the basics of decorators and mixins. Decorators add a new functionality to an object without changing other object instances of the same class, while a mixin is a kind of multiple inheritance used to inherit from multiple parent…
dev
  • 2,474
  • 7
  • 29
  • 47
23
votes
3 answers

An example of a mixin in Java?

On page 93-4 of Effective Java, I came across the term mixin. But I am finding it difficult to visualise what a mixin actually is. Could anybody please help me out by providing an example of a mixin in Java?
blackpanther
  • 10,998
  • 11
  • 48
  • 78
22
votes
1 answer

What is the UserMixin in Flask?

from datetime import datetime from werkzeug.security import generate_password_hash from werkzeug.security import check_password_hash from flask_login import UserMixin from app import db class User(UserMixin, db.Model): id =…
Sandip Swain
  • 417
  • 1
  • 4
  • 10