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?
Backbone definitely runs a bit of code on a
.triggercall. If you're keen, you can actually follow what happens by looking at the source code: https://github.com/jashkenas/backbone/blob/master/backbone.jsThe 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.