Slide 1

Slide 1 text

Rafael Felix Nohup - Tech Talk Backbone.JS

Slide 2

Slide 2 text

WTF Backbone?

Slide 3

Slide 3 text

• MV* WTF Backbone?

Slide 4

Slide 4 text

• MV* • Separação Lógica entre camadas WTF Backbone?

Slide 5

Slide 5 text

• MV* • Separação Lógica entre camadas • Evita código macarronico WTF Backbone?

Slide 6

Slide 6 text

How it works? Model Collection View Server

Slide 7

Slide 7 text

Backbone.Model var Product = Backbone.Model.extend({ urlRoot: '/products' });

Slide 8

Slide 8 text

Backbone.Model var Product = Backbone.Model.extend({ urlRoot: '/products' }); GET /products/:id PUT /products POST /products/:id DELETE /products/:id

Slide 9

Slide 9 text

Backbone.Model var Product = Backbone.Model.extend({ urlRoot: '/products' }); GET /products/:id PUT /products POST /products/:id DELETE /products/:id fetch save save destroy

Slide 10

Slide 10 text

Backbone.Collection var Products = Backbone.Collection.extend({ model: Product, url: '/products' });

Slide 11

Slide 11 text

Backbone.Collection var Products = Backbone.Collection.extend({ model: Product, url: '/products' }); GET /products

Slide 12

Slide 12 text

Backbone.Collection var Products = Backbone.Collection.extend({ model: Product, url: '/products' }); GET /products fetch

Slide 13

Slide 13 text

Backbone.View var ProductItemView = Backbone.View.extend({ tagName: 'li', className: 'product', initialize: function () { this.model.on('change', this.render, this); }, render: function () { this.$el.html(this.model.get('name')); return this; } });

Slide 14

Slide 14 text

Collection View var ProductsView = Backbone.View.extend({ tagName: 'ul', className: 'products', initialize: function () { this.collection.on('add', this.addOne, this); this.collection.on('reset', this.addAll, this); }, render: function () { return this; }, addAll: function () { this.$el.html(''); this.collection.forEach(this.addOne, this); }, addOne: function (product) { var view = new ProductItemView({ model: product }); this.$el.append(view.render().el); } });

Slide 15

Slide 15 text

Backbone.Router var App = new (Backbone.Router.extend({ routes: { 'products' : 'products' }, products: function () { var collection = new Products, view = new ProductsView({collection: collection}); $('#container').html(view.render().el); collection.fetch(); }, start: function () { Backbone.history.start({pushState: true}); } }));

Slide 16

Slide 16 text

Backbone.Router var App = new (Backbone.Router.extend({ routes: { 'products' : 'products' }, products: function () { var collection = new Products, view = new ProductsView({collection: collection}); $('#container').html(view.render().el); collection.fetch(); }, start: function () { Backbone.history.start({pushState: true}); } })); Com pushState ativado: ‘/products’ Sem pushState: ‘#products’

Slide 17

Slide 17 text

Backbone.Router var App = new (Backbone.Router.extend({ routes: { 'products' : 'products' }, products: function () { var collection = new Products, view = new ProductsView({collection: collection}); $('#container').html(view.render().el); collection.fetch(); }, start: function () { Backbone.history.start({pushState: true}); } })); $(function () { App.start(); });

Slide 18

Slide 18 text

Views Handling Events var ProductsView = Backbone.View.extend({ ... events: { 'click li' : 'open' } ... open: function (event) { //Código para abrir o registro } });

Slide 19

Slide 19 text

E SE...

Slide 20

Slide 20 text

$(function () { $.getJSON('/products.json', function (data) { var list = $('
    ').attr('class', 'products'); $.each(data, function (product) { var item = $('
  • '). attr('class', 'product'). val(product.name); list.append(item); }); $('#container').html(list); }); $('#container').on('click', 'ul.products li', function () { //Lógica de renderizar a página do produto }); });

    Slide 21

    Slide 21 text

    Obrigado @rs_felix http://fellix.github.com