Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Backbone.js

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

 Backbone.js

Nohup Tech Talk Backbone.js

Avatar for Rafael Felix

Rafael Felix

August 10, 2012
Tweet

More Decks by Rafael Felix

Other Decks in Programming

Transcript

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

    PUT /products POST /products/:id DELETE /products/:id fetch save save destroy
  2. 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; } });
  3. 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); } });
  4. 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}); } }));
  5. 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’
  6. 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(); });
  7. Views Handling Events var ProductsView = Backbone.View.extend({ ... events: {

    'click li' : 'open' } ... open: function (event) { //Código para abrir o registro } });
  8. $(function () { $.getJSON('/products.json', function (data) { var list =

    $('<ul></ul>').attr('class', 'products'); $.each(data, function (product) { var item = $('<li></li>'). 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 }); });