Slide 1

Slide 1 text

Backbone.js Rafael Felix

Slide 2

Slide 2 text

Rafael Felix @rs_felix http://fellix.github.com http://github.com/fellix http://www.hite.com.br

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Model Collection View Server

Slide 6

Slide 6 text

Backbone.Model var Product = Backbone.Model.extend({ urlRoot: ‘/products’ })

Slide 7

Slide 7 text

Backbone.Model var Product = Backbone.Model.extend({ urlRoot: ‘/products’ }) class Product < ActiveRecord::Base

Slide 8

Slide 8 text

Backbone.Model var Product = Backbone.Model.extend({ urlRoot: ‘/products’ })

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

Slide 10

Slide 10 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 11

Slide 11 text

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

Slide 12

Slide 12 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 13

Slide 13 text

No content

Slide 14

Slide 14 text

$(“#id”).append($(“

”).html(“teste”))

Slide 15

Slide 15 text

$(“#id”).append( $(“”).append( $(“”).append( $(“”).append( $(“”).html(“Header 1”) ).append( $(“”).html(“Header 2”) ) ) ).append($(“”).append( $(“”).append( $(“”).append(“Conteúdo 1”) ).append( $(“”).append(“Conteúdo 2”) ) ) )

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

app/assets/javascripts/templates/product.jst ... .... app/assets/javascripts/application.js JST[“templates/product”]()

Slide 18

Slide 18 text

Demonstração

Slide 19

Slide 19 text

class Product < ActiveRecord::Base validates :name, :description, :contact_email, presence: true scope :ordered, ->{ order("products.created_at asc") } end

Slide 20

Slide 20 text

class ProductsController < ApplicationController respond_to :json ... end

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

window.Product = Backbone.Model.extend({ urlRoot: "/products" }); window.Products = Backbone.Collection.extend({ model: Product, url: "/products" });

Slide 23

Slide 23 text

window.ProductApp = Backbone.View.extend({ el: $("#app"), initialize: function () { this.collection = new Products; }, render: function () { var list = new ProductList({ collection: this.collection }); this.$el.append(list.render().el); return this; } });

Slide 24

Slide 24 text

window.ProductApp = Backbone.View.extend({ el: $("#app"), initialize: function () { this.collection = new Products; }, render: function () { var list = new ProductList({ collection: this.collection }); this.$el.append(list.render().el); return this; } }); $(function () { new ProductApp().render() })

Slide 25

Slide 25 text

window.ProductList = Backbone.View.extend({ events: { "click #new-product": "newProduct" }, initialize: function () { this.collection.on("add", this.addItem, this); this.collection.fetch(); }, render: function () { this.$el.html('...'); return this; }

Slide 26

Slide 26 text

addItem: function (product) { var item = new ProductItem({ model: product }); this.$el.append(item.render().el); }, newProduct: function (evt) { evt.preventDefault(); var product = new Product; product.on("sync", function (model) { this.collection.add(model); }, this); var form = new ProductForm({ model: product }); form.show(); }

Slide 27

Slide 27 text

window.ProductItem = Backbone.View.extend({ className: "well span2", template: JST["templates/product_item"], events: { "click .more-info": "showDetails" }, initialize: function () { this.model.on("change", this.render, this); }, render: function () { this.$el.html(this.template(this.model.toJSON())); return this; }, showDetails: function () { var detail = new ProductDetail({ model: this.model }); detail.show(); } });

Slide 28

Slide 28 text

Slide 29

Slide 29 text

window.ProductForm = Backbone.View.extend({ ... events: { "click #commit": "commit" }, show: function () { this.$el.html(this.template()); this.$el.modal('show'); return this; }, ... });

Slide 30

Slide 30 text

commit: function (evt) { evt.preventDefault(); var view = this; this.clearErrors(); this.model.save({ name: this.$el.find("#name").val(), description: this.$el.find("#description").val(), contact_email: this.$el.find("#contact_email").val() }, { success: function (model, response, options) { view.close(); }, error: function (model, xhr, options) { var feedback = JSON.parse(xhr.responseText); _.each(feedback.errors, function (errors, field) { var errorField = view.$el.find('#'+field); errorField.parents("div.control-group").addClass("error"); errorField.parents("div.controls"). append("" + errors.join("\n") + ""); }); }

Slide 31

Slide 31 text

window.ProductDetail = Backbone.View.extend({ className: "modal fade", template: JST["templates/product_detail"], show: function () { this.$el.html(this.template(this.model.toJSON())); this.$el.modal('show'); return this; } });

Slide 32

Slide 32 text

@rs_felix Obrigado! https://github.com/fellix/rsonrails-backbone