Moff.event

This is event plugin which creates event system for modules communication between each other via events.

add(name)

Adds event into events storage. Usually all events are registered via this.events module's property.
And if you want to register new event dynamically use Moff.event.add method.

Arguments:

name - Event name.

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

	this.hasManyPosts = function() {
		return this.data.length;
	};

	this.init = function() {
		if (this.hasManyPosts) {
			Moff.event.add('onDelete');
		}
	};
});

get(name)

Get event callbacks from storage. It returns array of callbacks assigned for this event.

Returns array or undefined if event does not exist.

Arguments:

name - Event name.

Moff.modules.create('Comments', function() {
	this.handleAssigners = function() {
		var callbacks = Moff.event.get('afterPost');

		Moff.each(callbacks, function(index, callback) {
			// do something with callback...
		});
	};
});

off(name, callback)

Remove specified event handler or all handlers.

Arguments:

name - Event name.
[callback] - Function which will be removed.

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

	this.removeCommentBox = function() {
		// do something and remove all events of module
		Moff.event.off('beforeSend');
		Moff.event.off('afterSend');
	};
});

on(name, callback)

Assign callback for event. Event will be added if it is not added yet.

Arguments:

name - Event name to assign for.
callback - Function which will be executed on event trigger.

Moff.modules.create('Comments', function() {
	function loadComments() {

	}

	this.init = function() {
		Moff.$(function() {
			Moff.event.on('afterPost', loadComments);
		});
	};
});

once(name, callback)

Assign callback for event which will be executed one time and after it will be removed. Event will be added if it is not added yet.

Arguments:

name - Event name to assign for.
callback - Function which will be executed on event trigger one time.

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

	this.init = function() {
		var comments = this;
		Moff.event.once('afterMenuRender', function() {
			comments.getLinks();
		});
	};
});

trigger(name)

Trigger registered events and execute all assigned callbacks. You can pass any number of arguments after event name
and all of them will be passed to callbacks.

Arguments:

name - Event name.

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

	this.postComment = function() {
		var comments = this;
		Moff.ajax({
			type: 'POST',
			url: '/post/43/comments/',
			data: {userName: 'User Name', text: 'New comment text'},
			success: function(result) {
				Moff.event.trigger('afterPost', result);
			}
		);
	};
});