On Github tiagorg / turbinando-o-client-side-com-backbone-js
Tiago Romero Garcia
Tiago Romero Garcia
Gerente Técnico na Avenue Code Líder Técnico de Front-End Desenvolvedor Full-Stack
www.avenuecode.com.br Be part of the BEST!
Há muito tempo atrás, em uma galáxia muito, muito distante....
É tempo de guerra dos navegadores. Desenvolvedores rebeldes, atacando de uma base escondida, tiveram sua primeira vitória contra o temível Império da Microsoft.
Durante a batalha, espiões rebeldes conseguiram roubar os planos da arma definitiva do Império, o INTERNET EXPLORER 6, um navegador armado com poder suficiente para destruir uma conexão de Internet por completo.
Perseguido pelos agentes sinistros do Império, nossos heróis correm contra o tempo para diminuir o tempo de resposta e melhorar a navegação web para salvar os usuários e restaurar a liberdade na galáxia....
NÃO SÃO A MESMA COISA!
{
"name": "R2-D2",
"owners": ["Anakin", "Leia", "Luke"],
"attributes": {
"height": 0.96,
"mass": 32
}
}
MVC É O NOSSO HIPERESPAÇO!!
O BACKBONE.JS NÃO É UM FRAMEWORK MVC CLÁSSICO...
...MAS É DA FAMÍLIA MV*, COM OS MESMOS OBJETIVOS!!
var CardModel = Backbone.Model.extend({
urlRoot : '/api/cards'
});
var model = new CardModel({
id: 1
});
// GET /api/cards/1
model.fetch({
success: function(model, response, options) {
console.log('Sucesso no fetch! Model = ' + model.toJSON());
},
error: function(model, response, options) {
console.log('Erro ou timeout no fetch.');
}
});
var CardCollection = Backbone.Collection.extend({
model : CardModel,
url : '/api/cards'
});
var cardCollection = new CardCollection();
// GET /api/cards
cardCollection.fetch({
success: function(collection, response, options) {
console.log('Sucesso no fetch! Collection = ' + collection.toJSON());
},
error: function(collection, response, options) {
console.log('Erro ou timeout no fetch.');
}
});
var CardView = Backbone.View.extend({
events: {
'click button.close': function(e) {
e.preventDefault();
this.remove();
}
},
render: function() {
var result = '
var cardModel = new CardModel({ id: 1 });
cardModel.fetch({
success: function(model, response, options) {
var cardView = new CardView({
model: cardModel
);
cardView.render();
$('#contents').append(cardView.el);
},
error: function(model, response, options) {
console.log('Erro ou timeout no fetch.');
}
});
var AppRouter = Backbone.Router.extend({
routes : {
'' : 'viewAll',
'card/:id' : 'viewCard',
'*path' : 'viewAll'
},
viewAll: function() { ... },
viewCard: function(id) { ... }
});
var appRouter = new AppRouter();
Backbone.history.start({
pushState : true
});
{{#if children}}
Não há itens!
{{/if}}