Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Provides useful additions to Class

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 Source
Octocat-spinner-32 Specs
Octocat-spinner-32 .gitmodules
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 package.yml
README.md

Class Extras

Provides useful additions to Class.

Screenshot

This Plugin is part of MooTools PowerTools!.

Build

Build via Packager, requires MooTools Core to be registered to Packager already

packager register /path/to/class-extras
packager build Class-Extras/* > mootools-class-extras.js

To build this plugin without external dependencies use

packager build Class-Extras/* +use-only Class-Extras > class-extras.js

How To Use

Class.Binds

Provides an alternative way to bind class methods. Stores references to bound methods internally without any manual setup and does not modify the original methods.

new Class({

    Implements: Class.Binds,

    initialize: function(element){
        this.element = document.id(element);

        this.attach();
    },

    attach: function(){
        // Add the click method as event listener
        this.element.addEvent('click', this.bound('click'));
    },

    detach: function(){
        // Retrieves the same reference to the click method and removes the listener
        this.element.removeEvent('click', this.bound('click'));
    },

    click: function(event){
        event.preventDefault();

        // doSomething
        this.refersToTheClassInstance();
    }

})

Class.Singleton

Can be used to create a single instance of a class per context or per object/DOM-Element. Provides a "check" method to check for the existence of a class instance. Creates a class instance only via explicit "new" statement.

var MySingleton = new Class({

    Implements: Class.Singleton,

    initialize: function(){
        return this.check() || this.setup();
    },

    setup: function(){
        this.value = ...;
    }

});

new MySingleton === new MySingleton; // Returns the same instance.

Class.getInstanceOf(MySingleton); // Returns the previously created instance.

Class.Singleton can also be used with DOM-Elements to create a single instance of a class per DOM-Element.

var SinglePerElement = new Class({

    Implements: Class.Singleton,

    initialize: function(element){
        return this.check(element) || this.setup();
    },

    setup: function(){}

});

var myElement = document.id('someElement');

new SinglePerElement(myElement) === new SinglePerElement(myElement); // Same instance

myElement.getInstanceOf(SinglePerElement); // Returns the previously created element

(Note: DOM-Elements are not a requirement, any object with a store and retrieve method works)

Class.Instantiate

Useful for mass initialization of similar objects (usually DOM-Elements). Just a nice shortcut

var create = Class.Instantiate(MyClass, defaultOptions);

create($$('a')); // Creates one instance of MyClass for every a-tag. Initialize parameters are 'element, options'
Something went wrong with that request. Please try again.