Class.Delegates 0.5.1

Class Mutator. Exposes methods as its own by delegating specified method calls directly to specified elements within the Class.



Details

Author
Kevin Valdek
Current version
0.5.1
GitHub
kevinvaldek/class-delegates
Downloads
8523
Category
Utilities
Tags
Report
GitHub Issues

Releases


Dependencies

  • core/1.2.4: Class

How to use

All you have to do is to use Delegates in your class definition, and provide it with a object hash. Each key in the object is a delegation target. The value can be either the name of the delegated method, or an array of names if multiple methods should be delegated to the same element.

new Class({
    Delegates: {
        targetOne: 'methodName',
        targetTwo: ['methodNameOne', 'methodNameTwo', 'methodNameThree']
    }
});

Example

By delegating the method dispose to this.element, you could call the method dispose directly on the class instance instead of the element within the class (myInstance.dispose vs. myInstance.element.dispose).

var MyClass = new Class({
    initialize: function(element){
        this.element = document.id(element);
    },
    dispose: function(){
        this.element.dispose(); // manual delegation
    }
});

Class.Delegates saves you the trouble of delegating the dispose method call manually.

var MyClass = new Class({
    Delegates: {
        element: 'dispose' // target: methodName(s)
    },
    initialize: function(element){
        this.element = document.id(element);
    }
    // no dispose method needed anymore
});

By using Class.Delegates in the example above, you do not have to call dispose on the element anymore, but the class. Furthermore, dispose does not have to be implemented in the class.

Another example:

As the values in the Delegates object can either be a String or an Array, you can pass an array of method names to be delegated to the element (which is the key of the Delegates object). A more advanced example, here we delegate multiple methods to different elements:

var MyClass = new Class({
    Delegates: {
        element: 'dispose',
        peer: ['setStyle', 'grab', 'inject']
    },
    initialize: function(element, peer){
        this.element = document.id(element);
        this.peer = document.id(peer);
    }
});

By using Class.Delegates here, we can now call setStyle, grab and inject directly on an instance of MyClass, and each one of them gets delegated to this.peer.


Changelog

2010-02-03 - tag: 0.5.1 Dependencies added by henrikh.

2009-12-17 - tag: 0.5 Updates made by CrypticSwarm to get Class.Delegates to play nice with MooTools -core 1.2.4. Changes demod in MooShell can be found here: http://mooshell.net/gzj9e/1/ Tag 0.1 works with -core 1.2.1 but is not compatible with 1.2.4.


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