How to create a plugin

If you want to extend the functionality of DOMAssistant to suit your needs, you can create a plugin. Plugins work on top of the DOMAssistant core functionality, and are easily added.

Public and private methods

DOMAssistant offers you the approach of making a method either public or private. Public in this sense means that any public method will be publicly available through the $ and $$ methods on any element or object. Otherwise, it will only be available for your own internal use.

Specify your public methods, as strings representing the name of the actual method, in an array property of your plugin object named publicMethods. If no such property exists, all methods in your object will be considered public. If you don't want any of your methods to be public, set publicMethods to null or false.

Using an init method

If you have a method in your plugin named init, it will automatically be called once your public methods have been applied to the $ method. Please note that this doesn't necessarily mean that the document has finished loading, but just that your methods have been incorporated in the DOMAssistant functionality.

If you want to ensure that the document has finished loading before you do anything, use the DOMReady to accomplish that.

Eventual collision of method names

Say for example that you have named a method something which already exists in the core DOMAssistant or any other plugin included in the page before your plugin. What happens then?

The way DOMAssistant works is that it doesn't apply any method which already exists, meaning that if that's the case your method won't be applied. Please read the documentation of DOMAssistant and any other plugin you're using to prevent this from happening.

If you want to programmatically check if the method applied is the same method as your, you can check the allMethods property of the DOMAssistant object:

// Keyword this is here a reference to your plugin object 
if (DOMAssistant.allMethods["myMethodName"] === this["myMethodName"]) {
	// Now you know that your method was applied
}

An example plugin

Here is a simple plugin example:

DOMAssistant.MyPlugIn = function () {
	return {
		publicMethods : [
			"myMethod1",
			"myMethod2"
		],
		
		myMethod1 : function () {
			// Do something
		},
		
		myMethod2 : function () {
			// Do something else
		}
	};	
}();
DOMAssistant.attach(DOMAssistant.MyPlugIn);

An example plugin with comments in the code

Let's break a plugin down to see what each part does.

// Change the MyPlugIn part to the name of your plugin
DOMAssistant.MyPlugIn = function () {
	return {
		/* 
			These methods will be available through
			the $ and the $$ methods.
			If this property doesn't exist, all the object's 
			methods will be considered public.
			Set this to null or false to make all methods private.
			
			Like this:
			publicMethods : null,
		*/
		publicMethods : [
			"myMethod1",
			"myMethod2"
		],
		
		init : function () {
			/*
				This method is optional, and is automatically called 
				by DOMAssistant if it exists. The call takes place 
				after all the publicMethods have been applied.
			*/
		},
		
		myMethod1 : function () {
			/* 
				The keyword this in this context is 
				the element or object in question.

				return this is optional, if you want chaining 
				to work after this method has been called
				
				Like this: 
				$("#container").myMethod1().addClass("active");
			*/	
			return this; 
		},

		myMethod2 : function () {
			// Another public method
		}
	};	
}();
/*
	This line adds all the publicMethods of this plugin to the 
	DOMAssistant functionality, making them available through 
	the $ and the $$ methods.
	Remember to change the MyPlugIn part to the name of your plugin
*/
DOMAssistant.attach(DOMAssistant.MyPlugIn);

Download an example

Just download an example plugin and you're ready to get going creating fantastic DOMAssistant plugins!

Get your copy