Skip to content

Latest commit

 

History

History
80 lines (62 loc) · 2.6 KB

README.md

File metadata and controls

80 lines (62 loc) · 2.6 KB

Class.Delegates

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

Screenshot

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.

#JS
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).

#JS
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.

#JS
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:

#JS
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.