Mixin: Core/BeforeEvent

Core/BeforeEvent

BeforeEvent is a mixin that provides methods to create hooks that run before a certain event.

Usage Example:

const BeforeEvent = require('core/before-event');
_.extend(MyObject, BeforeEvent);

Methods


<static> before(name, callback [, context])

Adds a callback/hook to be fired before an action is taken. If that callback returns false, the action should not be taken.

The following example binds a callback function and passes the scope from the view component to use in that callback:

model.before('save', this.doSomethingBeforeSave, this);

Multiple space-separated event names can be bound to a single callback:

view.before('save dispose', this.callback, this);

This method also supports an event map syntax, as an alternative to positional arguments:

this.before({
    render: this.doSomethingBeforeRender,
    dispose: this.doSomethingBeforeDispose,
});
Parameters:
Name Type Argument Description
name string | Object

Event(s) to trigger before. Accepts multiple space-separated event names or an event map.

callback function

Function to be called.

context Object <optional>

Value to be assigned to this when the callback is fired.

Returns:

Instance of this class.

Type
Object

<static> offBefore( [name] [, callback] [, context])

Removes a previously-bound callback function from a before event.

If no context is given, all of the versions of the callback with different contexts will be removed:

this.offBefore('render', this.onRenderBefore);

If no callback is given, all callbacks for the before event will be removed:

this.offBefore('render');

If no event is specified, all callbacks for all before events will be removed from the object:

this.offBefore();
Parameters:
Name Type Argument Description
name string <optional>

Event(s) to remove the listeners for.

callback function <optional>

Callback to remove specifically for a given event.

context Object <optional>

Context to use when determining which callback to remove.

Returns:

Instance of this class.

Type
Object

<static> triggerBefore(name)

Triggers the before callback for the given event name or list of events.

The following example triggers the callback bound to the before save event given:

this.triggerBefore('save');

Multiple events can be triggered as well:

this.triggerBefore('save render dispose');

Custom arguments (e.g. a, b, c) can be passed to the callback:

this.triggerBefore('save', a, b, c);
Parameters:
Name Type Description
name string

The before event(s) to trigger.

Returns:

Returns true if the event should be triggered, false otherwise.

Type
boolean