Custom events
Library allows to generate its own events and track their occurrence via handlers. The following methods are used for this purpose.
Method | Description | Available from version |
---|---|---|
BX.addCustomEvent | Assigns handler to a custom event. | |
BX.removeCustomEvent | Deletes a custom event handler. | |
BX.onCustomEvent | Calls all event handlers for the object. | |
BX.PULL.extendWatch | Extension 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