New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

trickbag

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

trickbag

Add support for stacked mixins (separate behaviour) into any prototype.

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
3
200%
Maintainers
1
Weekly downloads
 
Created
Source

trickbag

Add support for stacked mixins (separate behaviour) into any prototype. Inspired by Vue & React mixins, and Backbone Cocktail.

It is useful to make your views/classes lighter and export common behavior in helpers. Some example use cases:

  • listening to the resize event and calling the appropriate method
  • listening to a mediator on object creation / unbinding the listener on object destruction
  • adding different behaviours on the same object hooks (init, render ...)
  • inheritance without super
  • ...

Installation

npm i trickbag --save

Usage

trickbag(object, [mixins...]);

  • mixins is an object (or an array of objects) containing methods; in other words, of mixins.
  • object is the object to apply these mixins to.

"Stacks"

Different mixins used on the same object can have methods with identical names. When the mixins are processed, trickbag properly stack all methods with the same name by wrapping them in a parent method, preserving the call order. For instance you can have an object with an init method. If your mixin also has the init method, it will be called after the object's one. That allows to easily plug behaviour on your custom classes lifecycle.

Example


var View = function(params) {
  trickbag(this, params.mixins);
};

var resizeMixin = {
  init: function() {
    this.onResize = this.onResize.bind(this);
    window.addEventListener('resize', this.onResize);
    console.log('Listening to resize')
  },

  onResize: function() {
    this.width = window.innerWidth;
    this.height = window.innerHeight;
  },

  destroy: function() {
    window.removeEventListener('resize', this.onResize);
  }
};

var fooMixin = {
  init: function() {
    console.log('Foo foo');
  }
}

var v = new View({
  mixins: [resizeMixin, fooMixin]
});

v.init(); // "Listening to resize", "Foo foo"


Keywords

mixin

FAQs

Package last updated on 28 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts