backbone.jsHow do I create a controller in Backbone.js?
Creating a controller in Backbone.js is done by extending the Backbone.Router object.
An example of a controller with a single route is as follows:
var AppRouter = Backbone.Router.extend({
routes: {
"": "home"
},
home: function(){
console.log("home route");
}
});
var app_router = new AppRouter();
Backbone.history.start();
This code will output home route
when the application is run.
Code explanation
-
var AppRouter = Backbone.Router.extend({
: This line creates a new router object and extends the Backbone.Router object. -
routes: { "": "home" },
: This line defines a single route, which is the home route. -
home: function(){ console.log("home route"); }
: This line defines the callback function for the home route, which logshome route
to the console. -
var app_router = new AppRouter();
: This line creates a new instance of the AppRouter object. -
Backbone.history.start();
: This line starts the routers history.
For more information on creating controllers in Backbone.js, see the Backbone.js documentation.
More of Backbone.js
- How can I create a WordPress website using Backbone.js?
- ¿Cuáles son las ventajas y desventajas de usar Backbone.js para el desarrollo de software?
- How can I decide between using Backbone.js or React.js for my software development project?
- How do I create a Backbone.js tutorial?
- How can I iterate over a collection in Backbone.js?
- "How can I tell if Backbone.js is still relevant?"
- How do I create a view in Backbone.js?
- How do I use the Backbone.js router to create a single-page application?
- How do Backbone.js and jQuery differ in their usage for software development?
- How can I use Backbone.js to create a web application according to Javatpoint tutorials?
See more codes...