Moff.Module

Moff.Module is a base class for extending during module creation via Moff.modules.create. Each created module will inherit all base class methods and properties.

afterInit()

This callback will be executed after module initialization.

Moff.modules.create('Comments', function() {
	this.afterInit = function() {
		// Callback logic...
	};
});

beforeInit()

This callback will be executed before module initialization. Passed to Moff.modules.initClass data is not available in this callback.

Moff.modules.create('Comments', function() {
	this.beforeInit = function() {
		// Callback logic...
	};
});

events

Array of events to be registered while module initialization.

Moff.modules.create('Comments', function() {
	this.events = ['beforePost', 'afterPost'];

	this.doSomething = function() {
		// Now 'beforePost' and 'afterPost' events are registered
		// and you can trigger assigned for them callbacks
		Moff.event.trigger('afterPost');
	};
});

find(selector)

Finds any DOM element inside module scope. In the example will be found .comment element located only inside module scope, even another one with the same class name will be located outside of the module scope.

Arguments:

selector - String containing one or more CSS selectors separated by commas.

Returns NodeList of elements matched by selector.

Moff.modules.create('Comments', function() {
	this.afterInit = function() {
		var firstComments = this.find('.comment:first');

		Moff.each(firstComments, function() {
			this.style.display = 'none';
		});
	};
});

init()

Module initialization callback.

Moff.modules.create('Comments', function() {
	this.init = function() {
		// Module initialization logic
	};
});

reopen(object)

Arguments:

object - Object with additional properties and methods.

Extends module base class with additional methods and properties. Also it can overwrite existing methods and properties.

Moff.Module.reopen({
	id: null,

	remove: function() {
		Moff.modules.remove(this);
	}
});

Now remove method available in all created modules.

Moff.modules.create('Comments', function {
	this.someMethod = function() {
		this.remove();
	};
});

scope

Module top DOM HTML element defined by scopeSelector. If scope selector wasn't defined scope will be null.

Returns HTML element .

Moff.module.register('Comments', function() {
	this.init = function() {
		this.scope.setAttribute('data-module-name', 'Comments');
	};
});

scopeSelector

String which contains CSS selector for module top DOM element. It is used on module initialization in setScope method to set moduel scope. By default it is null and if you do not set it, module scope will not be created.

Returns string.

Moff.module.register('Comments', function() {
	this.scopeSelector = '.post-comments';
});

setScope()

Creates module scope by scope selector. Scope sets on module initialization.
And if you want to change it you can call it with updated scope selector.

Moff.module.register('Comments', function() {
	this.afterInit = function() {
		this.scopeSelector = '.new-comments';
		this.setScope(); // now module scope is HTML element with new-comments class name.
	};
});