SlideShare a Scribd company logo
Aplicações
dinâmicas em
    Rails
Aplicacoes dinamicas Rails com Backbone
Rafael Felix


         @rs_felix


http://blog.rollingwithcode.com




  http://www.crafters.com.br
Aplicacoes dinamicas Rails com Backbone
Backbone é uma estrutura para
aplicações que fazem uso pesado de JavaScript, e
  conecta-se a sua aplicação por uma interface
                    RESTful.
Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com Backbone
Backbone.Model
var MyModel = Backbone.Model.extend({})
save([attributes],[options])


var MyModel = Backbone.Model.extend({})
POST           PUT


       save([attributes],[options])


var MyModel = Backbone.Model.extend({})
save([attributes],[options])


 var MyModel = Backbone.Model.extend({})


fetch([options])
save([attributes],[options])


 var MyModel = Backbone.Model.extend({})


fetch([options])     setInterval(function(){
                       model.fetch();
                     }, 10000);
save([attributes],[options])


 var MyModel = Backbone.Model.extend({})


fetch([options])            validate(attributes)
var Chapter = Backbone.Model.extend({
  validate: function(attrs) {
    if (attrs.end < attrs.start) {
      return "can't end before it starts";
    }
  }
});

 var one = new Chapter({
   title : "Chapter One: The Beginning"
 });

 one.bind("error", function(model, error) {
   alert(model.get("title") + " " + error);
 });

 one.set({
  start: 15,
  end:   10
 });
var Chapter = Backbone.Model.extend({
  validate: function(attrs) {
    if (attrs.end < attrs.start) {
      return "can't end before it starts";
    }
  }
});

 var one = new Chapter({
   title : "Chapter One: The Beginning"
 });

 one.bind("error", function(model, error) {
   alert(model.get("title") + " " + error);
 });

 one.set({
  start: 15,
  end:   10
 });
Backbone.Model
Backbone.Model




Backbone.Collection
var Library = Backbone.Collection.extend({
 model: Book
});
add(models, [options])

    var Library = Backbone.Collection.extend({
     model: Book
    });
add(models, [options])                    url()

    var Library = Backbone.Collection.extend({
     model: Book
    });
url: '/library'
add(models, [options])                     url()

    var Library = Backbone.Collection.extend({
     model: Book
    });
add(models, [options])                    url()

    var Library = Backbone.Collection.extend({
     model: Book
    });

  fetch([options])
add(models, [options])                     url()

    var Library = Backbone.Collection.extend({
     model: Book
    });

  fetch([options])       Library.fetch()

                         GET '/library'
add(models, [options])                       url()

    var Library = Backbone.Collection.extend({
     model: Book
    });

  fetch([options])       create(attributes, [options])
var alibrary = new Library;
var book = alibrary.create({
   title: "A book",
   author: "Someone"
})
Backbone.Model




Backbone.Collection
Backbone.Model




Backbone.Collection



                      Backbone.Router
var Workspace = Backbone.Router.extend({

 routes: {
  "help":                 "help",
  "search/:query":        "search",
  "search/:query/p:page": "search"
 },

 help: function() {},

 search: function(query, page) {}

});
var Workspace = Backbone.Router.extend({

 routes: {
  "help":                 "help",          #help
  "search/:query":        "search",        #search/felix
  "search/:query/p:page": "search"         #search/felix/p2
 },

 help: function() {},

 search: function(query, page) {}

});
var Workspace = Backbone.Router.extend({

 routes: {
  "help":                 "help",          #help
  "search/:query":        "search",        #search/felix
  "search/:query/p:page": "search"         #search/felix/p2
 },

 help: function() {},

 search: function(query, page) {}

});
var Workspace = Backbone.Router.extend({

 routes: {
  "help":                 "help",          #help
  "search/:query":        "search",        #search/felix
  "search/:query/p:page": "search"         #search/felix/p2
 },

 help: function() {},

 search: function(query, page) {}

});
var Workspace = Backbone.Router.extend({

 routes: {
  "help":                 "help",          #help
  "search/:query":        "search",        #search/felix
  "search/:query/p:page": "search"         #search/felix/p2
 },

 help: function() {},
                   felix  2
 search: function(query, page) {}

});
Backbone.Model




Backbone.Collection



                      Backbone.Router
Backbone.Model




          Backbone.Collection



Backbone.View                   Backbone.Router
var DocumentRow = Backbone.View.extend({

 tagName: "li",

 className: "document-row",

 events: {
  "click .icon": "open",
  "click .button.edit": "openEditDialog",
  "click .button.delete": "destroy"
 },

 render: function() {
   ...
 }

});
var DocumentRow = Backbone.View.extend({

 tagName: "li",
                                      <li class="document-row"></li>
 className: "document-row",

 events: {
  "click .icon": "open",
  "click .button.edit": "openEditDialog",
  "click .button.delete": "destroy"
 },

 render: function() {
   ...
 }

});
var DocumentRow = Backbone.View.extend({

 tagName: "li",

 className: "document-row",

 events: {
  "click .icon": "open",                    $(".icon").click(open)
  "click .button.edit": "openEditDialog",
  "click .button.delete": "destroy"
 },

 render: function() {
   ...
 }

});
Exemplo
Aplicacoes dinamicas Rails com Backbone
layout
layout
         application
layout
                   application
         ProductView
layout
                   application
         ProductView


                                 CartView
Aplicacoes dinamicas Rails com Backbone
click
click
Passo 1
layout
         application
app/views/layouts/application.html.erb


...
  <div class="container">
    <div class="content" id="application">
    </div>

    <footer>
      <p></p>
    </footer>

  </div>
...
JavaScript Templates
var obj = "bla bla bla";
someDiv = document.getElementById("someDiv");
someDiv.innerHTML = "<span>" + obj + "</span>";
template.jst

<span> ${obj} </span>
Aplicacoes dinamicas Rails com Backbone
template.jst

<span> ${obj} </span>
template.jst.ejs

<span> <%= obj %> </span>
app/assets/javascripts/templates/app.jst.ejs




      <div id="products" class="span10">
      </div>
      <div id="cart" class="span6">
      </div>
app/assets/javascripts/views/app_view.js


 window.AppView = Backbone.View.extend({
   template: JST["templates/app"],
   className: "row",

   initialize: function(){
   },

   render: function(){
     $(this.el).html(this.template());
     return this;
   }
 });
app/assets/javascripts/home.js




$(function(){
  view = new AppView().render().el;
  $(view).appendTo("#application");
});
app/assets/javascripts/home.js




             $(function(){
               view = new AppView().render().el;
               $(view).appendTo("#application");
             });



<div class="row">
  <div id="products" class="span12">
  </div>
  <div id="cart" class="span6">
  </div>
</div>
Passo 2
ProductView
app/assets/javascripts/templates/product.jst.ejs


<div class="product-image">
  <img class="thumbnail" src="http://placehold.it/90x90" alt="">
</div>
<div class="details">
  <span class="name"><%= model.get("name") %></span><br />
  <span class="price">R$ <%= model.get("price") %></span>
  <form class="add_product">
    <input type="hidden" name="id" value="<%= model.get("id") %>">
    <input type="submit" name="commit" value="Comprar" class="btn info">
  </form>
</div>
app/assets/javascripts/views/product_view.js


window.ProductView = Backbone.View.extend({
  template: JST["templates/product"],
  className: "product-detail",

  initialize: function(){
  },

  render: function(){
    $(this.el).html(this.template({model: this.model}));
    return this;
  }
});
rails g model product name:string price:decimal

           rails g controller products
         config/initializers/backbone.rb

   ActiveRecord::Base.include_root_in_json = false
rails g model product name:string price:decimal

           rails g controller products
         config/initializers/backbone.rb

   ActiveRecord::Base.include_root_in_json = false




           [
               {"product": { "name" : "" }},
               {"product": { "name": "" }},
           ]
rails g model product name:string price:decimal

           rails g controller products
         config/initializers/backbone.rb

   ActiveRecord::Base.include_root_in_json = false




           [
               {"name" : "" },
               {"name": "" },
           ]
app/controllers/products_controller.rb




class ProductsController < ApplicationController
  respond_to :json
  def index
    @products = Product.all
    respond_with @products
  end
end
app/assets/javascripts/models/product.js




window.Product = Backbone.Model.extend({

});

window.ProductsCollection = Backbone.Collections.extend({
  model: Product,
  url: '/products'
});
app/assets/javascripts/views/app_view.js

window.AppView = Backbone.View.extend({

  initialize: function(){
    _.bindAll(this, 'addOne', 'addAll');

       this.collection = new ProductsCollection;
       this.collection.bind('add', this.addOne);
       this.collection.bind('all', this.addAll);

       this.collection.fetch();
  },

  addAll: function(){
     $("#products").html("");
     this.collection.each(this.addOne);
  },

  addOne: function(product){
    view = new ProductView({model: product}).render().el;
    $(view).appendTo("#products");
  }
});
Aplicacoes dinamicas Rails com Backbone
Passo 3
CartView
app/assets/javascripts/templates/cart.jst.ejs




<div id="cart-products">
</div>
<hr/>
Total: <%= model.get("quantity") %> R$ <%= model.get("total") %>
app/assets/javascripts/views/cart_view.js

window.CartView = Backbone.View.extend({
  template: JST["templates/cart"],
  className: "cart-detail",

  initialize: function(){
     _.bindAll(this, 'render');
     this.model.bind('change', this.render);
     this.model.fetch();
  },

  render: function(){
    $(this.el).html(this.template({model: this.model}));
    return this;
  }
});
app/assets/javascripts/models/cart.js




 window.Cart = Backbone.Model.extend({
   url: function(){
     return '/cart';
   }
 });
rails g model cart quantity:integer total:decimal

             rails g controller cart
app/controllers/cart_controller.rb




class CartController < ApplicationController
  respond_to :json

  def show
    @cart ||= Cart.first || Cart.create!
    respond_with @cart
  end
end
app/controllers/cart_controller.rb




    get 'cart' => "cart#show"
class CartController < ApplicationController
  respond_to :json

  def show
    @cart ||= Cart.first || Cart.create!
    respond_with @cart
  end
end
app/assets/javascripts/views/app_view.js


window.AppView = Backbone.View.extend({
    ...
  initialize: function(){
    ...

    this.cart = new Cart;
    this.cartView = new CartView({model: this.cart}).
       render().el;
  },
  render: function(){
     $(this.el).html(this.template());
     this.$("#cart").html(this.cartView);
     return this;
  },
  ...
});
Aplicacoes dinamicas Rails com Backbone
Passo 4
click
click
rails g model cart_product cart:references product:references
rails g model cart_product cart:references product:references

                        app/models/cart.rb


 class Cart < ActiveRecord::Base
   has_many :cart_products
   has_many :products, through: :cart_products

   def add_product(product)
     self.update_attributes
        quantity: self.quantity + 1, total: total + product.price
     self.cart_products << CartProduct.new(cart: self, product: product)
   end
 end
app/assets/javascripts/models/cart_product.js




window.CartProduct = Backbone.Model.extend({
  url: function(){
     return "/cart/product/"+this.productId+"/add";
  },
  initialize: function(args){
     this.productId = args.productId;
  }
});
app/assets/javascripts/models/cart_product.js




window.CartProduct = Backbone.Model.extend({
  url: function(){
     return "/cart/product/"+this.productId+"/add";
  },
  initialize: function(args){
     this.productId = args.productId;
  }
});


    post 'cart/product/:id/add' => "cart#add_product"
app/controllers/cart_controller.rb


class CartController < ApplicationController
  respond_to :json

  def show
    ...
  end

  def add_product
    @product = Product.find params[:id]
    @cart = Cart.first
    @cart.add_product @product
    respond_with @cart
  end
end
app/assets/javascripts/views/product_view.js

 window.ProductView = Backbone.View.extend({
   ...

   events: {
      "submit form" : "addProductToCart"
   },

   initialize: function(args){
      ...
      this.cart = args.cart
   },

   ...

 });
app/assets/javascripts/views/product_view.js

              window.ProductView = Backbone.View.extend({
                ...

                 events: {
                    "submit form" : "addProductToCart"
                 },

                 initialize: function(args){
                    ...
                    this.cart = args.cart
                 },

                 ...
    app/assets/javascripts/views/app_view.js

              });
addOne: function(product){
  view = new ProductView({model: product, cart: this.cart}).render().el;
  $(view).appendTo("#products");
}
app/assets/javascripts/views/product_view.js

window.ProductView = Backbone.View.extend({
  ...

  addProductToCart: function(e){
    e.preventDefault();
    productId = this.$("form.add_product > input[name=id]").val();
    item = new CartProduct({productId: productId});
    view = this;
    item.save({}, {
      success: function(){
        view.cart.fetch();
      }
    });
  }
});
Aplicacoes dinamicas Rails com Backbone
http://backbone-todos.heroku.com/
Obrigado
                  felix.rafael@gmail.com
                http://twitter.com/rs_felix
                 http://github.com/fellix

                        Links
       http://documentcloud.github.com/backbone/
           https://github.com/creationix/haml-js
       https://github.com/codebrew/backbone-rails
http://seesparkbox.com/foundry/better_rails_apis_with_rabl

More Related Content

PDF
Ruby - Design patterns tdc2011
PDF
Backbone - TDC 2011 Floripa
PDF
Desenvolvendo APIs usando Rails - Guru SC 2012
PDF
Introduzione JQuery
KEY
Symfony2 Building on Alpha / Beta technology
PDF
Silex meets SOAP & REST
PPTX
Angular 2.0 Views
PDF
Javascript
Ruby - Design patterns tdc2011
Backbone - TDC 2011 Floripa
Desenvolvendo APIs usando Rails - Guru SC 2012
Introduzione JQuery
Symfony2 Building on Alpha / Beta technology
Silex meets SOAP & REST
Angular 2.0 Views
Javascript

What's hot (20)

PPTX
Hacking Your Way To Better Security - Dutch PHP Conference 2016
PPTX
jQuery from the very beginning
PDF
Min-Maxing Software Costs - Laracon EU 2015
PPTX
jQuery PPT
PDF
Angular.js Fundamentals
PDF
Design Patterns avec PHP 5.3, Symfony et Pimple
PDF
The History of PHPersistence
PDF
Database Design Patterns
KEY
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
PDF
Min-Maxing Software Costs
PPTX
AngularJS Services
PDF
Avinash Kundaliya: Javascript and WordPress
PDF
Introduction to Zend Framework web services
PDF
06 jQuery #burningkeyboards
PDF
Introduction to ReasonML
PDF
Perkenalan ReasonML
PDF
Decoupling with Design Patterns and Symfony2 DIC
PPTX
Template syntax in Angular 2.0
PDF
jQuery and Rails, Sitting in a Tree
PDF
05 JavaScript #burningkeyboards
Hacking Your Way To Better Security - Dutch PHP Conference 2016
jQuery from the very beginning
Min-Maxing Software Costs - Laracon EU 2015
jQuery PPT
Angular.js Fundamentals
Design Patterns avec PHP 5.3, Symfony et Pimple
The History of PHPersistence
Database Design Patterns
Building Single Page Apps with Backbone.js, Coffeescript and Rails 3.1
Min-Maxing Software Costs
AngularJS Services
Avinash Kundaliya: Javascript and WordPress
Introduction to Zend Framework web services
06 jQuery #burningkeyboards
Introduction to ReasonML
Perkenalan ReasonML
Decoupling with Design Patterns and Symfony2 DIC
Template syntax in Angular 2.0
jQuery and Rails, Sitting in a Tree
05 JavaScript #burningkeyboards
Ad

Similar to Aplicacoes dinamicas Rails com Backbone (20)

KEY
Backbone.js Simple Tutorial
PPTX
Backbonejs for beginners
PDF
Understanding backbonejs
ODP
Javascript frameworks: Backbone.js
KEY
Single Page Web Apps with Backbone.js and Rails
KEY
Prateek dayal backbonerails-110528024926-phpapp02
PDF
Introduction to Backbone.js for Rails developers
PDF
Backbone js
KEY
Backbone.js
PDF
An intro to Backbone.js
PDF
Javascript Application Architecture with Backbone.JS
PDF
Raybiztech Guide To Backbone Javascript Library
ODP
Backbone js
PPT
Backbone.js
PPT
Backbone js
KEY
Backbone intro
KEY
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
PDF
Client-side MVC with Backbone.js (reloaded)
PDF
Backbone
PDF
Viking academy backbone.js
Backbone.js Simple Tutorial
Backbonejs for beginners
Understanding backbonejs
Javascript frameworks: Backbone.js
Single Page Web Apps with Backbone.js and Rails
Prateek dayal backbonerails-110528024926-phpapp02
Introduction to Backbone.js for Rails developers
Backbone js
Backbone.js
An intro to Backbone.js
Javascript Application Architecture with Backbone.JS
Raybiztech Guide To Backbone Javascript Library
Backbone js
Backbone.js
Backbone js
Backbone intro
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Client-side MVC with Backbone.js (reloaded)
Backbone
Viking academy backbone.js
Ad

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Big Data Technologies - Introduction.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
KodekX | Application Modernization Development
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Empathic Computing: Creating Shared Understanding
Network Security Unit 5.pdf for BCA BBA.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Dropbox Q2 2025 Financial Results & Investor Presentation
Spectral efficient network and resource selection model in 5G networks
Big Data Technologies - Introduction.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
The AUB Centre for AI in Media Proposal.docx
20250228 LYD VKU AI Blended-Learning.pptx
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
Per capita expenditure prediction using model stacking based on satellite ima...
KodekX | Application Modernization Development
Diabetes mellitus diagnosis method based random forest with bat algorithm
Reach Out and Touch Someone: Haptics and Empathic Computing
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Advanced Soft Computing BINUS July 2025.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
NewMind AI Monthly Chronicles - July 2025
Empathic Computing: Creating Shared Understanding

Aplicacoes dinamicas Rails com Backbone