Class-Extras 1.0.5

Provides useful additions to Class.

This Plugin is part of MooTools PowerTools!.



Details

Author
Christoph Pojer
Current version
1.0.5
GitHub
cpojer/mootools-class-extras
Downloads
2343
Category
Utilities
Tags
Report
GitHub Issues

Releases


Dependencies

  • _self_/_current_:
    • Core/Class
    • Core/Function

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'

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

Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus