Кастомные события
Описание и методы
Библиотека позволяет генерировать свои собственные события и отслеживать их возникновение при помощи обработчиков. Для этого применяются следующие методы.
| Метод | Описание | С версии |
|---|---|---|
| BX.addCustomEvent | Назначает обработчик кастомному событию. | |
| BX.removeCustomEvent | Удаляет обработчик кастомного события. | |
| BX.onCustomEvent | Вызывает все обработчики события для объекта. | |
| BX.PULL.extendWatch | Продление подписки на тег. (Работает в модуле Push and Pull) | 14.0 |
Примеры
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();
© «Битрикс», 2001-2025, «1С-Битрикс», 2025