Henrique Silvério
JavaScript Developer
Front-End Developer na HE:labs
Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
backbonejs.org
Underscore ~ underscorejs.org
jQuery ~ jquery.com
#mimimi não use jQuery... ( http://goo.gl/QR8e13 )
Alternativas:
Lo-Dash ~ lodash.com
Zepto ~ zeptojs.com
Models are the heart of any JavaScript application, containing the interactive data as well as a large part of the logic surrounding it: conversions, validations, computed properties, and access control.
http://backbonejs.org/#Model
var Book = Backbone.Model.extend({
defaults: {
title: '',
author: ''
}
});
var Book = Backbone.Model.extend({});
var book1 = new Book({ title: 'Full Stack Web Development with Backbone.js' });
console.log( book1.get('title') );
// => 'Full Stack Web Development with Backbone.js'
book1.set('title', 'Backbone.js Cookbook');
console.log( book1.get('title') );
// => 'Backbone.js Cookbook'
book1.set({
title: 'Building Backbone Plugins',
author: 'Derick Bailey'
});
book1.toJSON();
// => Object {title: "Building Backbone Plugins", author: "Derick Bailey"}
Collections are ordered sets of models.
http://backbonejs.org/#Collection
var Library = Backbone.Collection.extend({
model: Book
});
Instanciando e populando:
var MyLibrary = new Library([
{ title: 'Developing Backbone.js Applications', author: 'Addy Osmani' },
{ title: 'Full Stack Web Development with Backbone.js ', author: 'Patrick Mulder' },
{ title: 'Building Backbone Plugins', author: 'Derick Bailey' }
]);
MyLibrary.at(0).toJSON();
// => 'Developing Backbone.js Applications'
MyLibrary.pluck('author');
// => Array [ "Addy Osmani", "Patrick Mulder", "Derick Bailey" ]
MyLibrary.where({ author: 'Addy Osmani' });
// => 'Developing Backbone.js Applications'
MyLibrary.length
// => 3
Organize your interface into logical views, backed
(or not)by models...
http://backbonejs.org/#View
var BookView = Backbone.View.extend({
tagName: 'div',
className: 'book-item',
events: {
'click .open': 'openClicked'
},
initialize: function() {
console.log('The View has created!');
},
openClicked: function(e) {
console.log( $(e.target) );
}
});
var theBook = new BookView();
// => 'The View has created!'
var BookView = Backbone.View.extend({
// ... tagName, className, events ...
template: _.template( [
'<b>Title:</b> <%= title %> <br>',
'<b>Author:</b> <%= author %> <hr>'
].join('') ),
render: function() {
var data = this.model.toJSON();
this.$el.html( this.template( data ) );
return this;
},
// ... initialize and other methods ...
});
var theBookView = new BookView({ model: theBookModel });
$('body').html( theBookView.render().el );
var theBookModel = new Book({
title: 'Backbone.Marionette.js: A Gentle Introduction',
author: 'David Sulc'
});
var LibraryView = Backbone.View.extend({
tagName: 'ul',
className: 'books-list',
render: function() {
this.collection.each(function(book) {
var bookItemView = new BookView({ model: book });
this.$el.append( bookItemView.render().el );
}, this);
return this;
}
});
var BooksCollection = new Library([
{ title: 'Developing Backbone.js Applications', author: 'Addy Osmani' },
{ title: 'Full Stack Web Development with Backbone.js ', author: 'Patrick Mulder' },
{ title: 'Building Backbone Plugins', author: 'Derick Bailey' },
{ title: 'Backbone.Marionette.js: A Gentle Introduction', author: 'David Sulc' }
]);
var theLibraryView = new LibraryView({ collection: BooksCollection });
$('body').html( theLibraryView.render().el );
Events is a module that can be mixed in to any object, giving the object the ability to bind and trigger custom named events.
http://backbonejs.org/#Events
var Library = Backbone.Collection.extend({
model: Book,
selectByCID: function(cid) {
this.invoke('set', 'selected', false);
var book = this.get(cid);
if(book) {
book.set('selected', true);
}
return this;
}
});
var BookView = Backbone.View.extend({
events: {
'click': 'selectBook'
},
initialize: function() {
this.listenTo( this.model, 'change:selected', this.render );
},
render: function() {
var data = this.model.toJSON();
this.$el.html( this.template( data ) );
this.$el.toggleClass( 'selected', this.model.get('selected') );
return this;
},
selectBook: function(e) {
if(!this.model.get('selected')) {
this.model.collection.selectByCID( this.model.cid );
}
}
});
// ... tagName, className, template ...
Backbone.Router provides methods for routing client-side pages, and connecting them to actions and events.
http://backbonejs.org/#Router
var AppRouter = Backbone.Router.extend({
routes: {
'': 'showHome',
'book/:id' : 'showBook'
},
showBook: function(id) {
// ... get the book by id
// ... create and render a view
// etc
}
});
$(function() {
var appRouter = new AppRouter();
Backbone.history.start();
});
A curated compilation of resources to master Backbone.js development.
By Henrique Silvério
Slides da palestra ministrada na "Semana de TI" na faculdade Anhanguera em Bauru, dia 28/10/2015.