dotmixin

simple mixin implementation

npm install dotmixin
8 downloads in the last month

Description

Simple mixin implementation.. basically conveniences around functional mixins as described in Angus Croll's "A fresh look at JavaScript Mixins"

Latest Version

0.0.2

Installation

npm install dotmixin

or in package.json

{
  ...
  "dependencies": {
    "dotmixin": "0.0.x"
  }
}

Usage

Add a "mixin" method to a source like this

var Mixin = require('dotmixin');

function Blah (options) {
  this.blah = function(text) {
    return (options.greeting + ' ' + text);
  };
}
Mixin(Blah);

module.exports = Blah;

and then mix that into a target like this

var Blah = require('./Blah');

function Barf () {}
Blah.mixin(Barf, { greeting: 'wow...' });

barf = new Barf();
barf.blah('barf!!');  // 'wow... barf!!'

It's also possible to use the Mixin.mixin method directly

var Mixin = require('dotmixin');

function Foo () {
  this.foo = function() {
    return 'food';
  };
}

function Duh () {}
Mixin.mixin(Foo, Duh);

duh = new Duh();
duh.foo() // 'food'

Notes / TODO

Mixin sources should be a function accepting a single "options" object as an argument that provides for mixin-level configuration.

At some point would be cool to have webpage listing sources (probably just subset of npm modules that have "dotmixin" as a package.json keyword)

Sources should probably also be augmented with a .describeMixin function that returns a list of the properties they add to the target. When that function is called with an argument name, show details about that function/property and its arguments.

For the example above..

Blah.describeMixin() // ['blah']
Blah.describeMixin('blah') // { type: Function, arguments: ['text'] }

Should develop convention in the options argument to allow for aliasing of the added functions & properties to avoid namespace collisions.

How to handle dependencies? Should it be possible to only mixin a subset of properties?

Build status

build status

npm loves you