The Backbone.JS Event listenToOnce() method is used to inform one object to listen to another object’s event, same as that of Event listenTo() method. When an event occurs, it also provides a callback function. It also keep a track for the events. The only difference between the Event listenToOnce() method and the Event listenTo() method is that the Event listenToOnce() method causes the listenTo method to occur only once and after that the callback function is removed.
Syntax:
object.listenToOnce(other, event, callback)
Parameters:
other: This parameter is used to specify the name of the other object.
event: This parameter is used to specify the event whose binding with an object is needed.
callback: This parameter is used to specify the callback function that will be executed when an event is fired.
Example:
Example
Output:
The triggered message for you is: How are you? This is an example for Event listenToOnce() method.
Explanation:
The Event listenToOnce() method will inform one object to listen to another object’s event. When an event occurs, it provides a callback function. Here, the object Y listens once for the ‘func’ event triggered on the object X. The X has no ‘func’ event and thus displays the value of Y.