CodesJava

Easy learning with example program codes

Backbone.JS Router Execute()


The Backbone.JS Router Execute() method is used when a route matches its corresponding callback.

Syntax:

Backbone.Router.Execute (callback, args)   

Parameters:
callback: This parameter is used to specify the function to be executed when the route matches.
args: This parameter is used to specify the arguments to be passed with the method.

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>  
<script type="text/javascript">  
var X = Backbone.View.extend({  
template: '<b>Route 1</b>',  
initialize: function () {  
this.execute();  
},  
execute: function () {  
this.$el.html(this.template);  
}  });  
var Y = Backbone.View.extend({  
template: '<b>Route 2</b>',  
initialize: function () {  
this.execute();  
},  
execute: function () {  
this.$el.html(this.template);  
}  
});  
var Z = Backbone.Router.extend({  
routes: {  
'': 'Route_1',  
'route/1': 'Route_1',  
'route/2': 'Route_2',  
},  
homeRoute: function () {  
var x = new X();  
$("#content").html(x.el);  
},  
aboutRoute: function () {  
var y = new Y();  
$("#content").html(y.el);  
}  
});  
var z = new Z();     
Backbone.history.start();  
</script>  
<body>  
<div id="navigation">  
<ul>  
<li> <a href="#/route1">Route 1 </a> </li>  
<li> <a href="#/route2">Route 2 </a> </li>  
</ul>  
</div>  
<div id="content"></div>  
</body>  
</html>

Output:

●	Route 1
●	Route 2

Explanation:
In the above example the Execute() method is called internally when route matches its callback.

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki