Backbone.js trigger() Event

The Backbone.js event trigger method invokes the callback functions for the mentioned events.

Syntax:

object.trigger(event, arg)

Parameters: event: This parameter is used to specify the event whose binding with an object is needed. arg: It is an optional parameter that is used to specify the values or arguments to be passed to the callback function.

Example:

<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"
type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"
type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
var X = _.extend({msg:'Hello World'}, Backbone.Events);
X.on('func', function () {
document.write("Triggered message: ");
document.write(this.msg);
});
X.trigger('func');
</script>
</body>
</html>

Explanation: The on() method will bind the event to an object. The trigger() method will than invoke the function which will execute in return and thus the message will be displayed.

Please follow and like us:
Content Protection by DMCA.com