backbone.jsHow can I build an application using Backbone.js?
Backbone.js is a JavaScript library that makes it easier to create dynamic, single-page web applications. It provides an MVC (Model View Controller) framework that helps to organize code and keep it modular. To build an application using Backbone.js, you need to include the library in your HTML page and create models, views, and controllers.
Example code
<script src="backbone.js"></script>
// Model
var Person = Backbone.Model.extend({
defaults: {
name: '',
age: 0
}
});
// View
var PersonView = Backbone.View.extend({
tagName: 'li',
render: function() {
this.$el.html(this.model.get('name') + ' (' + this.model.get('age') + ')');
return this;
}
});
// Controller
var PeopleController = Backbone.Router.extend({
routes: {
"people/:name": "showPerson"
},
showPerson: function(name) {
var person = new Person({name: name, age: 30});
var personView = new PersonView({model: person});
$('#people').html(personView.render().el);
}
});
var peopleController = new PeopleController();
Backbone.history.start();
This code creates a Person
model, a PersonView
view, and a PeopleController
router. The Person
model has two properties, name
and age
, and the PersonView
view renders the model in an HTML li
element. The PeopleController
router has a single route, people/:name
, which will render the PersonView
with the name specified in the route. Finally, the Backbone.history.start()
call enables the router.
Code explanation
- Include the Backbone.js library in the HTML page:
<script src="backbone.js"></script>
- Create a model:
var Person = Backbone.Model.extend({...});
- Create a view:
var PersonView = Backbone.View.extend({...});
- Create a controller:
var PeopleController = Backbone.Router.extend({...});
- Create a route in the controller:
routes: { "people/:name": "showPerson" }
- Create a controller action:
showPerson: function(name) {...}
- Start the router:
Backbone.history.start();
Helpful links
More of Backbone.js
- How do I use W3Schools to learn Backbone.js?
- How do I use a template engine with Backbone.js?
- How can I use Backbone.js with React to build a web application?
- How do I create tabs using Backbone.js?
- How do I create a todo list application using Backbone.js?
- How do I use a trigger in Backbone.js?
- How can I use Backbone.js to fetch query strings?
- How do I pass a collection to a Backbone.js view?
- How do I organize the structure of a Backbone.js project?
- How can I use Backbone.js to wait for a fetch request to complete?
See more codes...