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
5102
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();
    }

})
  1. new Class({
  2. Implements: Class.Binds,
  3. initialize: function(element){
  4. this.element = document.id(element);
  5. this.attach();
  6. },
  7. attach: function(){
  8. // Add the click method as event listener
  9. this.element.addEvent('click', this.bound('click'));
  10. },
  11. detach: function(){
  12. // Retrieves the same reference to the click method and removes the listener
  13. this.element.removeEvent('click', this.bound('click'));
  14. },
  15. click: function(event){
  16. event.preventDefault();
  17. // doSomething
  18. this.refersToTheClassInstance();
  19. }
  20. })

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.
  1. var MySingleton = new Class({
  2. Implements: Class.Singleton,
  3. initialize: function(){
  4. return this.check() || this.setup();
  5. },
  6. setup: function(){
  7. this.value = ...;
  8. }
  9. });
  10. new MySingleton === new MySingleton; // Returns the same instance.
  11. 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
  1. var SinglePerElement = new Class({
  2. Implements: Class.Singleton,
  3. initialize: function(element){
  4. return this.check(element) || this.setup();
  5. },
  6. setup: function(){}
  7. });
  8. var myElement = document.id('someElement');
  9. new SinglePerElement(myElement) === new SinglePerElement(myElement); // Same instance
  10. 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'
  1. var create = Class.Instantiate(MyClass, defaultOptions);
  2. 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
  1. packager register /path/to/class-extras
  2. 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
  1. 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