Moff.modules

This is modularity system API which works with modules. It allows to create, remove, find, initialize and other operations with them.

get(name)

Gets initialized via Moff.modules.initClass module class by name. Object is returned if the only class object was initialized. If more then one class objects were initialized it returns array of objects. If none of class objects were initialized it returns undefined.

Arguments:

name - Module name.

Returns object, array or undefined .

Moff.modules.create('Comments', function() {});

Moff.modules.initClass('Comments', {id: 'comments_1', data: [{}, {}, {}]});
Moff.modules.get('Comments'); // Comments{}

Moff.modules.initClass('Comments', {id: 'comments_2', data: [{}, {}, {}]});
Moff.modules.get('Comments'); // [Comments{}, Comments{}]

Moff.modules.get('NonExistingClass'); // undefined

getAll()

Gets all initialized module instances.

Returns object.

Moff.modules.create('Comments', function() {
	this.handleAllModule = function() {
		var all = Moff.modules.getAll(); // {Comments: [Comments{}, Comments{}], Post: Post{}}

		Moff.each(all, function(name, object) {
			// do something with object...
		});
	};
});

getBy(property, value)

Gets initialized module instances by specified property.

Arguments:

property - Property name.
value - Property value.

Returns array.

Moff.modules.create('Comments', function() {});
Moff.modules.initClass('Comments', {id: 'comments_1'});
Moff.modules.initClass('Comments', {id: 'comments_2'});

Moff.modules.getBy('id', 'comments_1'); // [Comments{id="comments_1"}]

// Also you can get by class name
Moff.modules.getBy('class', 'Comments'); // [Comments{id="comments_1"}, Comments{id="comments_2"}]

initClass(ClassName, params)

Initialize registered class object. While class initialization this method does the following:

  • Checks whether passed class name is registered. If not throw warning and return.
  • If dependencies were registered it loads them only then starts initialization.
  • Creates new class object and stores in storage.
  • Runs beforeInit callbacks if exists.
  • Apply passed params to new created object.
  • Add module name to moduleName property.
  • Registers module events.
  • Sets module scope.
  • Call init method.
  • And execute afterInit method.

Arguments:

ClassName - Registered class name.
[params] - Object with additional params which will be applied as class properties.

// comments.js
Moff.modules.create('Comments', function() {
	this.scopeSelector = '.comments';

	this.events = ['beforePost', 'afterPost'];

	this.beforeInit = function() {

	};

	this.init = function() {

	};

	this.afterInit = function() {

	};
});

// HTML document or different js file
Moff.modules.initClass('Comments', {id: 'comments_43'});

create(name, depends, Constructor)

Creates new Moff.Module class into the module storage. Depends files will be loaded on class initialization via Moff.module.initClass method. If you register another module with the same name it will overwrite existing one.

Arguments:

name - Registered class name.
[depends] - Object of js and css files.
Constructor - Class constructor.

Moff.modules.create(
	'Comments',
	{
		js: ['js/plugins/facebook-comments.js'],
		css: ['js/plugins/facebook-comments.css']
	},
	function() { /** Constructor */ }
);

remove(module)

Removes module instance by passed name or instance. This method removes only initialized object classes. So you are still can initialize it again.

Arguments:

module - Module name or class instance.

Moff.modules.create('Comments', function() {});
Moff.modules.remove('Comments'); // Remove by module class name

var comments = Moff.modules.get('Comments');
Moff.modules.remove(comments); // Remove by class instance