backbone, is there a side effect to triggering and event that there is no listeners for

44 Views Asked by At

So in backbone when we go to trigger and event lets say

this.trigger("do:something")

what if there are no listeners for this, obviously there are no errors thrown, but are there any side effects, otherwise I would need to wrap this in a conditional?

2

There are 2 best solutions below

0
arbuthnott On

Backbone definitely runs a bit of code on a .trigger call. If you're keen, you can actually follow what happens by looking at the source code: https://github.com/jashkenas/backbone/blob/master/backbone.js

The code could amount to a conditional early return if the object has no events defined. Or it could involve iterating over arrays of possible callbacks if any listeners have been added.

If you have a simple to check conditional then likely it would run faster than calling an empty .trigger, though the difference would surely never be noticeable.

If your conditional makes your code ugly, or is based on another backbone library method, I would skip it.

0
AudioBubble On

There are no side effects as such but having such functions and given there are many can lead to slowing down the application.