Documentation

Custom events

Library allows to generate its own events and track their occurrence via handlers. The following methods are used for this purpose.

MethodDescriptionAvailable from version
BX.addCustomEventAssigns handler to a custom event.
BX.removeCustomEventDeletes a custom event handler.
BX.onCustomEventCalls all event handlers for the object.
BX.PULL.extendWatchExtension of the tag subscription. (Functions in the Push and Pull module)

Example

function Animal(name, diet)
{
    this.name = name;
    this.diet = diet == 'carnivore' ? 'carnivore' : 'herbivore';
}

Animal.prototype.Hungry = function()
{
    if(this.diet == 'carnivore')
        this.Hunt();
    else
        this.Pasture();
}

Animal.prototype.Hunt = function()
{
    BX.addCustomEvent('onAnimalPasture', BX.proxy(function(prey){
        console.log(this.name + ' catches ' + prey.name);
        this.Eat(prey.name);
    }, this));
}

Animal.prototype.Pasture = function()
{
    this.Eat('grass');
    BX.onCustomEvent(this, 'onAnimalPasture', [this]);
}

Animal.prototype.Eat = function(food)
{
    console.log(this.name + ' eats ' + food);
}

var wolf = new Animal('wolf', 'carnivore'),
    deer = new Animal('deer', 'herbivore');

wolf.Hungry();
deer.Hungry();


© «Bitrix24», 2001-2024