CodesJava

Easy learning with example program codes

Backbone.js trigger() Event

The Backbone.js event trigger method is used to invoke 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 which 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>

Output:

Triggered message: Hello World

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:
error
Posted in Java   








Copyright © 2019 CodesJava DMCA.com Protection Status