SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
Globalcode	
  – Open4education
Interfaces  ricas  com  Rails  e  React.JS
Rodrigo  Urubatan
@urubatan
Globalcode	
  – Open4education
Quem?
Programador desde 1997
Trabalha com Ruby na Brightwire
Escreveu "Ruby On Rails: Desenvolvimento fácil e Rápido de aplicações web”
Já trabalhou com diversas linguagens (C, C++, Delphi, PHP, ASP, ColdFusion,
VisualBasic, C#, Python, Ruby, Assembly, …)
Apaixonado por Agile e atualmente por trabalho remoto
Patinador, Corredor, Ciclista e agora resolveu aprender Karate :D
Pai de um Guri de 6 anos
http://urubatan.com.br - Anda meio abandonado mas vai voltar
http://sobrecodigo.com - idem
Globalcode	
  – Open4education
Objetivo (O blog mais feio do
mundo!)
Globalcode	
  – Open4education
Objetivo
Usar o Rails como backend da aplicação
Toda a interação com o usuário será implementada no cliente
Validações e regras de negócio serão implementadas no servidor
(sim, eu poderia usar Sinatra mas sou preguiçoso)
Globalcode	
  – Open4education
Cuidado!
Nesta palestra vamos falar de uma SPA (Single Page Application)
Isto tem vantagens e desvantagens
Melhora muito a interação com o usuário sem duplicação de código, já que o
código de renderização fica todo no JS
Piora muito a indexação da sua aplicação por buscadores (adeus SEO - ou
não…)
Globalcode	
  – Open4education
Criando a aplicação
Uma aplicação padrão Rails (rails new …)
Gemfile updates
gem 'backbone-on-rails'
gem 'react-rails', github: 'reactjs/react-rails', ref:
'master'
Environment update (development.rb para começar)
config.react.variant = :development
config.react.addons = true # defaults to false
config.react.server_renderer =
React::ServerRendering::SprocketsRenderer
bundle install
rails g react:install
Globalcode	
  – Open4education
Componentes
Componentes javascript vão ficar em app/assets/javascript/components
Backbone.js vai facilitar a comunicação cliente/servidor
arquivos .js.jsx tem uma facilidade extra, são compilados pelo react-rails via asset
pipeline e permitem adicionar HTML inline
Globalcode	
  – Open4education
Cadastrando um Usuário
rails g scaffold post title:string slug:text content:text
Apagar todas as views do post exceto index.html.erb
Apagar todo o código de index.html.erb e mudar para:
<%= react_component('Blog', {collection: @posts, item: @post}) %>
Globalcode	
  – Open4education
Alterações no controller
Fazer todos os métodos retornarem json, mais ou menos assim (não é a melhor
tecnica, mas boa o suficiente para o exemplo)
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
def index
@posts = Post.all
end
def show
@posts = Post.all
render :index
end
# GET /posts/new
def new
@post = Post.new
render :index
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.json { render json: @post, status: :created, location: @post }
else
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
#  PATCH/PUT  /posts/1
#  PATCH/PUT  /posts/1.json
def update
respond_to do  |format|
if  @post.update(post_params)
format.json {  render  json:  @user,  status:  :ok,  location:  @post  }
else
format.json {  render  json:  @post.errors,  status:  :unprocessable_entity }
end
end
end
#  DELETE  /posts/1
#  DELETE  /posts/1.json
def destroy
@post.destroy
respond_to do  |format|
format.json {  head  :no_content }
end
end
private
#  Use  callbacks  to  share  common  setup  or  constraints  between  actions.
def set_post
@post  =  Post.find_by(slug:  params[:id])  ||  Post.find(params[:id])
end
#  Never  trust  parameters  from  the  scary  internet,  only  allow  the  white  list  through.
def post_params
params.require(:post).permit(:title,  :slug,  :content)
end
end
Globalcode	
  – Open4education
Agora mãos a obra
já temos uma “API" em Rails, poderíamos ter o código em Sinatra que seria mais
leve, mas eu gosto do asset pipeline e assim fica mais fácil para um iniciante
Falta criar os componentes backbone para acessar o backend
Criar os componentes react para a UI
Globalcode	
  – Open4education
backbone.js
app/assets/javascripts/collections/posts.js
var Posts = Backbone.Collection.extend({
model: Post,
url: '/posts'
});
app/assets/jaascripts/models/post.js
var Post = Backbone.Model.extend({
});
Globalcode	
  – Open4education
blog.js.jsx
var Blog = React.createClass({
getInitialState: function(){
var posts = null;
var post = null;
if (this.props.collection) {
posts = new Posts(this.props.collection);
} else {
posts = new Posts();
posts.fetch({success:function(data){
this.forceUpdate();
}.bind(this)});
}
if (this.props.item) {
post = new Post(this.props.item);
} else {
post = new Post();
}
return {
collection: posts,
model: post,
editing: false
};
},
componentDidMount:function(){
this.router = new PostRouter({blog: this});
Backbone.history.start({pushState: true, root: "/"});
},
editModel:function(model){
this.setState({
model: model,
editing: true
})
},
viewModel:function(model){
this.setState({
model:  model,
editing:  false
});;
this.router.navigate("/posts/"  +  model.get("slug"),  {trigger:  true});;
},
newModel:function(){
this.setState({
model:  new  Post(),
editing:  true
})
},
render:  function   ()  {
if(this.state.editing)  {
return  (<div>
<div  id="blogList">
<PostList collection={this.state.collection}  viewModel={this.viewModel}  
newModel={this.newModel}/>
</div>
<div  id="blogPost">
<PostForm collection={this.state.collection}  model={this.state.model}  
viewModel={this.viewModel}/>
</div>
</div>);;
}else{
return  (<div>
<div  id="blogList">
<PostList collection={this.state.collection}  viewModel={this.viewModel}  
newModel={this.newModel}/>
</div>
<div  id="blogPost">
<PostView model={this.state.model}  editModel={this.editModel}/>
</div>
</div>);;
}
}
});;
Globalcode	
  – Open4education
post_list.js.jsx
var PostList = React.createClass({
componentDidMount: function () {
this.props.collection.on("change", function () {
this.forceUpdate()
}, this);
this.props.collection.on("reset", function () {
this.forceUpdate()
}, this);
},
componentWillUnmount: function () {
this.props.collection.off(null, null, this);
},
render: function () {
var users = this.props.collection.map(function (model) {
var viewModel = function () {
this.props.viewModel(model);
};
return (
<tr key={model.get("id")}>
<td><a href="#" onClick={viewModel.bind(this)}>{model.get("title")}</a></td>
<td>{new Date(model.get("created_at")).toDateString()}</td>
</tr>
);
}.bind(this));
return (
<div>
<table className="post-list">
<thead>
<tr>
<th>Post</th>
<th>Published</th>
</tr>
</thead>
<tbody>
{users}
</tbody>
</table>
<hr/>
<a href="#" onClick={this.props.newModel}>Create post</a>
</div>
);
}
});
Globalcode	
  – Open4education
post_view.js.jsx
var PostView = React.createClass({
editModel: function () {
this.props.editModel(this.props.model);
},
render: function () {
var innerLines = null;
if(this.props.model.get("content")) {
innerLines=_.map(this.props.model.get("content").split("n"), function (txt, idx) {
return <p key={idx}>{txt}</p>
});
}
return (
<div className="blog-post">
<h1><a href={this.props.model.get("slug")}>{this.props.model.get("title")}</a></h1>
<div className="post-body">
{innerLines}
</div>
<hr/>
<a href="#" onClick={this.editModel}>Edit post</a>
</div>
);
}
});
Globalcode	
  – Open4education
post_form.js.jsx
var PostForm = React.createClass({
saveModel: function () {
if (this.props.model.get("id")) {
this.props.model.save();
} else {
this.props.collection.create(this.props.model);
}
this.props.viewModel(this.props.model)
},
render: function () {
return (
<div className="blog-post">
<InputWithLabel model={this.props.model} label="Title" name="title" type="text"/>
<InputWithLabel model={this.props.model} label="Body" name="content" type="textarea"/>
<div className="form-field">
<button onClick={this.saveModel}>Save</button>
</div>
</div>
);
}
});
Globalcode	
  – Open4education
input_with_label.js.jsx
var InputWithLabel = React.createClass({
handleChange: function(event) {
this.props.model.set(this.props.name,event.target.value)
},
render: function() {
return <div className="form-field">
<label htmlFor={this.props.name}>{this.props.label}</label>
<div>
<input id={this.props.name} type={this.props.type} name={this.props.name} ref="input"
onChange={this.handleChange} value={this.props.model.get(this.props.name)}/>
</div>
</div>;
}
});
Globalcode	
  – Open4education
O que, quando, onde e por
que?
Muitas aplicações hoje em dia exigem um nível alto de interação com o usuário
Implementar isto usando bibliotecas mais baixo nível é muito fácil de causar
uma grande confusão do código (PHP alguem?)
Componentização evita duplicação de código e facilita a organização
Globalcode	
  – Open4education
Globalcode	
  – Open4education
indexação? performance?
renderização no servidor:
<%= react_component('Blog', {collection: @posts, item: @post},
{prerender: true}) %>
components.js
//= require underscore
//= require backbone
//= require_tree ./models
//= require_tree ./collections
//= require_tree ./components
Globalcode	
  – Open4education
Mas é só isto?
React-router
Backbone.Router
Flux - arquitetura JS usada pelo Facebook
Pré processamento com Gulp (sintaxe do ECMAScript 6 suportada)
Globalcode	
  – Open4education

Mais conteúdo relacionado

Mais procurados

Oficina groovy grails - infoway
Oficina  groovy grails - infowayOficina  groovy grails - infoway
Oficina groovy grails - infowayLucas Aquiles
 
Grails: O Java em Alta Produtividade
Grails: O Java em Alta ProdutividadeGrails: O Java em Alta Produtividade
Grails: O Java em Alta ProdutividadeCleórbete Santos
 
ZF2 básico : Desenvolvendo um Blog com o Zend Framework 2
ZF2 básico : Desenvolvendo um Blog com o Zend Framework 2ZF2 básico : Desenvolvendo um Blog com o Zend Framework 2
ZF2 básico : Desenvolvendo um Blog com o Zend Framework 2Cezar Souza
 
Começando com Zend Framework 2
Começando com Zend Framework 2Começando com Zend Framework 2
Começando com Zend Framework 2Cezar Souza
 
WEB 2 - Aula 01 - 02.08
WEB 2 - Aula 01 - 02.08WEB 2 - Aula 01 - 02.08
WEB 2 - Aula 01 - 02.08Gilson Silva
 
Java Web 3 - Servlets e JSP 1
Java Web 3 - Servlets e JSP 1Java Web 3 - Servlets e JSP 1
Java Web 3 - Servlets e JSP 1Eduardo Mendes
 
Desenvolvimento para a Web com CakePHP
Desenvolvimento para a Web com CakePHPDesenvolvimento para a Web com CakePHP
Desenvolvimento para a Web com CakePHPMarcelo Andrade
 
Zend Framework 1.11
Zend Framework 1.11Zend Framework 1.11
Zend Framework 1.11Cezar Souza
 
Java Web 2 - Ferramentas e configuração
Java Web 2 - Ferramentas e configuraçãoJava Web 2 - Ferramentas e configuração
Java Web 2 - Ferramentas e configuraçãoEduardo Mendes
 
Beyond Ruby with NodeJS - RubyConf Brasil 2010
Beyond Ruby with NodeJS - RubyConf Brasil 2010Beyond Ruby with NodeJS - RubyConf Brasil 2010
Beyond Ruby with NodeJS - RubyConf Brasil 2010Emerson Macedo
 
Desenvolvimento web em java com JSP e Servlets
Desenvolvimento web em java com JSP e ServletsDesenvolvimento web em java com JSP e Servlets
Desenvolvimento web em java com JSP e ServletsIgo Coelho
 
Mini curso: Ionic Framework
Mini curso: Ionic FrameworkMini curso: Ionic Framework
Mini curso: Ionic FrameworkLoiane Groner
 
Workshop Ruby on Rails dia 2 ruby-pt
Workshop Ruby on Rails dia 2  ruby-ptWorkshop Ruby on Rails dia 2  ruby-pt
Workshop Ruby on Rails dia 2 ruby-ptPedro Sousa
 
Desenvolvimento Front end (AngularJS e Bootstrap)
Desenvolvimento Front end (AngularJS e Bootstrap)Desenvolvimento Front end (AngularJS e Bootstrap)
Desenvolvimento Front end (AngularJS e Bootstrap)Julian Cesar
 

Mais procurados (20)

Oficina groovy grails - infoway
Oficina  groovy grails - infowayOficina  groovy grails - infoway
Oficina groovy grails - infoway
 
Groovy grails
Groovy grailsGroovy grails
Groovy grails
 
Grails: O Java em Alta Produtividade
Grails: O Java em Alta ProdutividadeGrails: O Java em Alta Produtividade
Grails: O Java em Alta Produtividade
 
ZF2 básico : Desenvolvendo um Blog com o Zend Framework 2
ZF2 básico : Desenvolvendo um Blog com o Zend Framework 2ZF2 básico : Desenvolvendo um Blog com o Zend Framework 2
ZF2 básico : Desenvolvendo um Blog com o Zend Framework 2
 
Começando com Zend Framework 2
Começando com Zend Framework 2Começando com Zend Framework 2
Começando com Zend Framework 2
 
WEB 2 - Aula 01 - 02.08
WEB 2 - Aula 01 - 02.08WEB 2 - Aula 01 - 02.08
WEB 2 - Aula 01 - 02.08
 
Servlets e jsp
Servlets e jspServlets e jsp
Servlets e jsp
 
Java Web 3 - Servlets e JSP 1
Java Web 3 - Servlets e JSP 1Java Web 3 - Servlets e JSP 1
Java Web 3 - Servlets e JSP 1
 
Desenvolvimento para a Web com CakePHP
Desenvolvimento para a Web com CakePHPDesenvolvimento para a Web com CakePHP
Desenvolvimento para a Web com CakePHP
 
Angular js
Angular jsAngular js
Angular js
 
Zend Framework 1.11
Zend Framework 1.11Zend Framework 1.11
Zend Framework 1.11
 
Java Web 2 - Ferramentas e configuração
Java Web 2 - Ferramentas e configuraçãoJava Web 2 - Ferramentas e configuração
Java Web 2 - Ferramentas e configuração
 
Beyond Ruby with NodeJS - RubyConf Brasil 2010
Beyond Ruby with NodeJS - RubyConf Brasil 2010Beyond Ruby with NodeJS - RubyConf Brasil 2010
Beyond Ruby with NodeJS - RubyConf Brasil 2010
 
Grails
GrailsGrails
Grails
 
Aula Ruby
Aula RubyAula Ruby
Aula Ruby
 
Desenvolvimento web em java com JSP e Servlets
Desenvolvimento web em java com JSP e ServletsDesenvolvimento web em java com JSP e Servlets
Desenvolvimento web em java com JSP e Servlets
 
Mini curso: Ionic Framework
Mini curso: Ionic FrameworkMini curso: Ionic Framework
Mini curso: Ionic Framework
 
Introdução ao vraptor
Introdução ao vraptorIntrodução ao vraptor
Introdução ao vraptor
 
Workshop Ruby on Rails dia 2 ruby-pt
Workshop Ruby on Rails dia 2  ruby-ptWorkshop Ruby on Rails dia 2  ruby-pt
Workshop Ruby on Rails dia 2 ruby-pt
 
Desenvolvimento Front end (AngularJS e Bootstrap)
Desenvolvimento Front end (AngularJS e Bootstrap)Desenvolvimento Front end (AngularJS e Bootstrap)
Desenvolvimento Front end (AngularJS e Bootstrap)
 

Destaque

Rails Concerns and Turbolinks
Rails Concerns and TurbolinksRails Concerns and Turbolinks
Rails Concerns and TurbolinksNascenia IT
 
Real-Time Rails: Implementing WebSockets in Rails 5 with Action Cable
Real-Time Rails: Implementing WebSockets in Rails 5 with Action CableReal-Time Rails: Implementing WebSockets in Rails 5 with Action Cable
Real-Time Rails: Implementing WebSockets in Rails 5 with Action CableSophie DeBenedetto
 
ウェブチップス勉強会 Action cable
ウェブチップス勉強会 Action cableウェブチップス勉強会 Action cable
ウェブチップス勉強会 Action cableYu Ito
 
What's new in Rails 5 - API Mode & Action Cable overview
What's new in Rails 5 - API Mode & Action Cable overviewWhat's new in Rails 5 - API Mode & Action Cable overview
What's new in Rails 5 - API Mode & Action Cable overviewMaxim Veksler
 
Real Time with Rails 5
Real Time with Rails 5Real Time with Rails 5
Real Time with Rails 5Lucas Renan
 
Ruby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesRuby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesPhraseApp
 
Rails 5 – Amsterdam.rb – Uberous
Rails 5 – Amsterdam.rb – UberousRails 5 – Amsterdam.rb – Uberous
Rails 5 – Amsterdam.rb – UberousJeroen Visser
 
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?Srijan Technologies
 

Destaque (11)

Rails Concerns and Turbolinks
Rails Concerns and TurbolinksRails Concerns and Turbolinks
Rails Concerns and Turbolinks
 
Efficient rails
Efficient railsEfficient rails
Efficient rails
 
Real-Time Rails: Implementing WebSockets in Rails 5 with Action Cable
Real-Time Rails: Implementing WebSockets in Rails 5 with Action CableReal-Time Rails: Implementing WebSockets in Rails 5 with Action Cable
Real-Time Rails: Implementing WebSockets in Rails 5 with Action Cable
 
ウェブチップス勉強会 Action cable
ウェブチップス勉強会 Action cableウェブチップス勉強会 Action cable
ウェブチップス勉強会 Action cable
 
What's new in Rails 5 - API Mode & Action Cable overview
What's new in Rails 5 - API Mode & Action Cable overviewWhat's new in Rails 5 - API Mode & Action Cable overview
What's new in Rails 5 - API Mode & Action Cable overview
 
Real Time with Rails 5
Real Time with Rails 5Real Time with Rails 5
Real Time with Rails 5
 
Ruby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 FeaturesRuby on Rails 5: Top 5 Features
Ruby on Rails 5: Top 5 Features
 
Rails 5 – Amsterdam.rb – Uberous
Rails 5 – Amsterdam.rb – UberousRails 5 – Amsterdam.rb – Uberous
Rails 5 – Amsterdam.rb – Uberous
 
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
[Srijan Wednesday Webinar] Rails 5: What's in It for Me?
 
What's New in Rails 5
What's New in Rails 5What's New in Rails 5
What's New in Rails 5
 
What's new on Rails 5
What's new on Rails 5What's new on Rails 5
What's new on Rails 5
 

Semelhante a Interfaces ricas com Rails e React.JS @ Rubyconf 2015

TDC São Paulo 2015 - Interfaces Ricas com Rails e React.JS
TDC São Paulo 2015  - Interfaces Ricas com Rails e React.JSTDC São Paulo 2015  - Interfaces Ricas com Rails e React.JS
TDC São Paulo 2015 - Interfaces Ricas com Rails e React.JSRodrigo Urubatan
 
JavaScript Model-View no Frontend
JavaScript Model-View no FrontendJavaScript Model-View no Frontend
JavaScript Model-View no FrontendHenrique Gogó
 
Evento Front End SP - Organizando o Javascript
 Evento Front End SP - Organizando o Javascript Evento Front End SP - Organizando o Javascript
Evento Front End SP - Organizando o JavascriptMichel Ribeiro
 
Programando para programadores: Desafios na evolução de um Framework
Programando para programadores: Desafios na evolução de um FrameworkProgramando para programadores: Desafios na evolução de um Framework
Programando para programadores: Desafios na evolução de um FrameworkPablo Dall'Oglio
 
Evolução e futuro do uso de paradigmas no JavaScript
Evolução e futuro do uso de paradigmas no JavaScriptEvolução e futuro do uso de paradigmas no JavaScript
Evolução e futuro do uso de paradigmas no JavaScriptJean Carlo Emer
 
Desenvolvimento ágil com Kohana framework
Desenvolvimento ágil com Kohana frameworkDesenvolvimento ágil com Kohana framework
Desenvolvimento ágil com Kohana frameworkMarcelo Rodrigo
 
Introduzindo StimulusJS: o novo Framework JavaScript para Ruby On Rails.
Introduzindo StimulusJS: o novo Framework JavaScript para Ruby On Rails.Introduzindo StimulusJS: o novo Framework JavaScript para Ruby On Rails.
Introduzindo StimulusJS: o novo Framework JavaScript para Ruby On Rails.Sergio Lima
 
Construindo portlets para IBM WebSphere Portal – Parte 2
Construindo portlets para IBM WebSphere Portal – Parte 2Construindo portlets para IBM WebSphere Portal – Parte 2
Construindo portlets para IBM WebSphere Portal – Parte 2rodrigoareis
 
Curso de Ruby on Rails - Aula 01
Curso de Ruby on Rails - Aula 01Curso de Ruby on Rails - Aula 01
Curso de Ruby on Rails - Aula 01Maurício Linhares
 
Aplicações rápidas para a Web com Django
Aplicações rápidas para a Web com DjangoAplicações rápidas para a Web com Django
Aplicações rápidas para a Web com DjangoFreedom DayMS
 
Criando APIs com Node e TypeScript
Criando APIs com Node e TypeScriptCriando APIs com Node e TypeScript
Criando APIs com Node e TypeScriptAndre Baltieri
 
Workshop Node.js + MongoDB + Mongoose
Workshop Node.js + MongoDB + MongooseWorkshop Node.js + MongoDB + Mongoose
Workshop Node.js + MongoDB + MongooseLuiz Duarte
 
Desenvolvimento de Sistemas Web com PHP Frameworks - 2019.1 - Aula 1
Desenvolvimento de Sistemas Web com PHP Frameworks - 2019.1 - Aula 1Desenvolvimento de Sistemas Web com PHP Frameworks - 2019.1 - Aula 1
Desenvolvimento de Sistemas Web com PHP Frameworks - 2019.1 - Aula 1Thyago Maia
 
Backbone.js + Rails - Front-end e back-end conectados
Backbone.js + Rails - Front-end e back-end conectadosBackbone.js + Rails - Front-end e back-end conectados
Backbone.js + Rails - Front-end e back-end conectadosHenrique Gogó
 
Introducao ao Spring Web MVC
Introducao ao Spring Web MVCIntroducao ao Spring Web MVC
Introducao ao Spring Web MVCEder Magalhães
 

Semelhante a Interfaces ricas com Rails e React.JS @ Rubyconf 2015 (20)

TDC São Paulo 2015 - Interfaces Ricas com Rails e React.JS
TDC São Paulo 2015  - Interfaces Ricas com Rails e React.JSTDC São Paulo 2015  - Interfaces Ricas com Rails e React.JS
TDC São Paulo 2015 - Interfaces Ricas com Rails e React.JS
 
JavaScript Model-View no Frontend
JavaScript Model-View no FrontendJavaScript Model-View no Frontend
JavaScript Model-View no Frontend
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Evento Front End SP - Organizando o Javascript
 Evento Front End SP - Organizando o Javascript Evento Front End SP - Organizando o Javascript
Evento Front End SP - Organizando o Javascript
 
Programando para programadores: Desafios na evolução de um Framework
Programando para programadores: Desafios na evolução de um FrameworkProgramando para programadores: Desafios na evolução de um Framework
Programando para programadores: Desafios na evolução de um Framework
 
Evolução e futuro do uso de paradigmas no JavaScript
Evolução e futuro do uso de paradigmas no JavaScriptEvolução e futuro do uso de paradigmas no JavaScript
Evolução e futuro do uso de paradigmas no JavaScript
 
Desenvolvimento ágil com Kohana framework
Desenvolvimento ágil com Kohana frameworkDesenvolvimento ágil com Kohana framework
Desenvolvimento ágil com Kohana framework
 
Introduzindo StimulusJS: o novo Framework JavaScript para Ruby On Rails.
Introduzindo StimulusJS: o novo Framework JavaScript para Ruby On Rails.Introduzindo StimulusJS: o novo Framework JavaScript para Ruby On Rails.
Introduzindo StimulusJS: o novo Framework JavaScript para Ruby On Rails.
 
Construindo portlets para IBM WebSphere Portal – Parte 2
Construindo portlets para IBM WebSphere Portal – Parte 2Construindo portlets para IBM WebSphere Portal – Parte 2
Construindo portlets para IBM WebSphere Portal – Parte 2
 
Curso de Ruby on Rails - Aula 01
Curso de Ruby on Rails - Aula 01Curso de Ruby on Rails - Aula 01
Curso de Ruby on Rails - Aula 01
 
Aplicações rápidas para a Web com Django
Aplicações rápidas para a Web com DjangoAplicações rápidas para a Web com Django
Aplicações rápidas para a Web com Django
 
Criando APIs com Node e TypeScript
Criando APIs com Node e TypeScriptCriando APIs com Node e TypeScript
Criando APIs com Node e TypeScript
 
Workshop Node.js + MongoDB + Mongoose
Workshop Node.js + MongoDB + MongooseWorkshop Node.js + MongoDB + Mongoose
Workshop Node.js + MongoDB + Mongoose
 
Desenvolvimento de Sistemas Web com PHP Frameworks - 2019.1 - Aula 1
Desenvolvimento de Sistemas Web com PHP Frameworks - 2019.1 - Aula 1Desenvolvimento de Sistemas Web com PHP Frameworks - 2019.1 - Aula 1
Desenvolvimento de Sistemas Web com PHP Frameworks - 2019.1 - Aula 1
 
Backbone.js + Rails - Front-end e back-end conectados
Backbone.js + Rails - Front-end e back-end conectadosBackbone.js + Rails - Front-end e back-end conectados
Backbone.js + Rails - Front-end e back-end conectados
 
Php 05 Mvc
Php 05 MvcPhp 05 Mvc
Php 05 Mvc
 
Introducao ao Spring Web MVC
Introducao ao Spring Web MVCIntroducao ao Spring Web MVC
Introducao ao Spring Web MVC
 
Solid
SolidSolid
Solid
 
Zend Framework
Zend FrameworkZend Framework
Zend Framework
 
Model View Controller
Model View ControllerModel View Controller
Model View Controller
 

Mais de Rodrigo Urubatan

Data science in ruby is it possible? is it fast? should we use it?
Data science in ruby is it possible? is it fast? should we use it?Data science in ruby is it possible? is it fast? should we use it?
Data science in ruby is it possible? is it fast? should we use it?Rodrigo Urubatan
 
Data science in ruby, is it possible? is it fast? should we use it?
Data science in ruby, is it possible? is it fast? should we use it?Data science in ruby, is it possible? is it fast? should we use it?
Data science in ruby, is it possible? is it fast? should we use it?Rodrigo Urubatan
 
2018 the conf put git to work - increase the quality of your rails project...
2018 the conf   put git to work -  increase the quality of your rails project...2018 the conf   put git to work -  increase the quality of your rails project...
2018 the conf put git to work - increase the quality of your rails project...Rodrigo Urubatan
 
2018 RubyHACK: put git to work - increase the quality of your rails project...
2018 RubyHACK:  put git to work -  increase the quality of your rails project...2018 RubyHACK:  put git to work -  increase the quality of your rails project...
2018 RubyHACK: put git to work - increase the quality of your rails project...Rodrigo Urubatan
 
TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis ...
TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis ...TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis ...
TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis ...Rodrigo Urubatan
 
Your first game with unity3d framework
Your first game with unity3d frameworkYour first game with unity3d framework
Your first game with unity3d frameworkRodrigo Urubatan
 
Tdc Floripa 2017 - 8 falácias da programação distribuída
Tdc Floripa 2017 -  8 falácias da programação distribuídaTdc Floripa 2017 -  8 falácias da programação distribuída
Tdc Floripa 2017 - 8 falácias da programação distribuídaRodrigo Urubatan
 
Rubyconf2016 - Solving communication problems in distributed teams with BDD
Rubyconf2016 - Solving communication problems in distributed teams with BDDRubyconf2016 - Solving communication problems in distributed teams with BDD
Rubyconf2016 - Solving communication problems in distributed teams with BDDRodrigo Urubatan
 
resolvendo problemas de comunicação em equipes distribuídas com bdd
resolvendo problemas de comunicação em equipes distribuídas com bddresolvendo problemas de comunicação em equipes distribuídas com bdd
resolvendo problemas de comunicação em equipes distribuídas com bddRodrigo Urubatan
 
vantagens e desvantagens de trabalhar remoto
vantagens e desvantagens de trabalhar remotovantagens e desvantagens de trabalhar remoto
vantagens e desvantagens de trabalhar remotoRodrigo Urubatan
 
Using BDD to Solve communication problems
Using BDD to Solve communication problemsUsing BDD to Solve communication problems
Using BDD to Solve communication problemsRodrigo Urubatan
 
Full Text Search com Solr, MySQL Full text e PostgreSQL Full Text
Full Text Search com Solr, MySQL Full text e PostgreSQL Full TextFull Text Search com Solr, MySQL Full text e PostgreSQL Full Text
Full Text Search com Solr, MySQL Full text e PostgreSQL Full TextRodrigo Urubatan
 
Ruby para programadores java
Ruby para programadores javaRuby para programadores java
Ruby para programadores javaRodrigo Urubatan
 
Treinamento html5, css e java script apresentado na HP
Treinamento html5, css e java script apresentado na HPTreinamento html5, css e java script apresentado na HP
Treinamento html5, css e java script apresentado na HPRodrigo Urubatan
 
Ruby on rails impressione a você mesmo, seu chefe e seu cliente
Ruby on rails  impressione a você mesmo, seu chefe e seu clienteRuby on rails  impressione a você mesmo, seu chefe e seu cliente
Ruby on rails impressione a você mesmo, seu chefe e seu clienteRodrigo Urubatan
 
Aplicações Hibridas com Phonegap e HTML5
Aplicações Hibridas com Phonegap e HTML5Aplicações Hibridas com Phonegap e HTML5
Aplicações Hibridas com Phonegap e HTML5Rodrigo Urubatan
 
Git presentation to some coworkers some time ago
Git presentation to some coworkers some time agoGit presentation to some coworkers some time ago
Git presentation to some coworkers some time agoRodrigo Urubatan
 
Intrudução ao Behavior Driven Development (BDD) com Ruby on Rails
Intrudução ao Behavior Driven Development (BDD) com Ruby on RailsIntrudução ao Behavior Driven Development (BDD) com Ruby on Rails
Intrudução ao Behavior Driven Development (BDD) com Ruby on RailsRodrigo Urubatan
 

Mais de Rodrigo Urubatan (20)

Ruby code smells
Ruby code smellsRuby code smells
Ruby code smells
 
Data science in ruby is it possible? is it fast? should we use it?
Data science in ruby is it possible? is it fast? should we use it?Data science in ruby is it possible? is it fast? should we use it?
Data science in ruby is it possible? is it fast? should we use it?
 
Data science in ruby, is it possible? is it fast? should we use it?
Data science in ruby, is it possible? is it fast? should we use it?Data science in ruby, is it possible? is it fast? should we use it?
Data science in ruby, is it possible? is it fast? should we use it?
 
2018 the conf put git to work - increase the quality of your rails project...
2018 the conf   put git to work -  increase the quality of your rails project...2018 the conf   put git to work -  increase the quality of your rails project...
2018 the conf put git to work - increase the quality of your rails project...
 
2018 RubyHACK: put git to work - increase the quality of your rails project...
2018 RubyHACK:  put git to work -  increase the quality of your rails project...2018 RubyHACK:  put git to work -  increase the quality of your rails project...
2018 RubyHACK: put git to work - increase the quality of your rails project...
 
TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis ...
TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis ...TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis ...
TDC2017 - POA - Aprendendo a usar Xamarin para desenvolver aplicações moveis ...
 
Your first game with unity3d framework
Your first game with unity3d frameworkYour first game with unity3d framework
Your first game with unity3d framework
 
Tdc Floripa 2017 - 8 falácias da programação distribuída
Tdc Floripa 2017 -  8 falácias da programação distribuídaTdc Floripa 2017 -  8 falácias da programação distribuída
Tdc Floripa 2017 - 8 falácias da programação distribuída
 
Rubyconf2016 - Solving communication problems in distributed teams with BDD
Rubyconf2016 - Solving communication problems in distributed teams with BDDRubyconf2016 - Solving communication problems in distributed teams with BDD
Rubyconf2016 - Solving communication problems in distributed teams with BDD
 
resolvendo problemas de comunicação em equipes distribuídas com bdd
resolvendo problemas de comunicação em equipes distribuídas com bddresolvendo problemas de comunicação em equipes distribuídas com bdd
resolvendo problemas de comunicação em equipes distribuídas com bdd
 
vantagens e desvantagens de trabalhar remoto
vantagens e desvantagens de trabalhar remotovantagens e desvantagens de trabalhar remoto
vantagens e desvantagens de trabalhar remoto
 
Using BDD to Solve communication problems
Using BDD to Solve communication problemsUsing BDD to Solve communication problems
Using BDD to Solve communication problems
 
Full Text Search com Solr, MySQL Full text e PostgreSQL Full Text
Full Text Search com Solr, MySQL Full text e PostgreSQL Full TextFull Text Search com Solr, MySQL Full text e PostgreSQL Full Text
Full Text Search com Solr, MySQL Full text e PostgreSQL Full Text
 
Ruby para programadores java
Ruby para programadores javaRuby para programadores java
Ruby para programadores java
 
Treinamento html5, css e java script apresentado na HP
Treinamento html5, css e java script apresentado na HPTreinamento html5, css e java script apresentado na HP
Treinamento html5, css e java script apresentado na HP
 
Ruby on rails impressione a você mesmo, seu chefe e seu cliente
Ruby on rails  impressione a você mesmo, seu chefe e seu clienteRuby on rails  impressione a você mesmo, seu chefe e seu cliente
Ruby on rails impressione a você mesmo, seu chefe e seu cliente
 
Mini curso rails 3
Mini curso rails 3Mini curso rails 3
Mini curso rails 3
 
Aplicações Hibridas com Phonegap e HTML5
Aplicações Hibridas com Phonegap e HTML5Aplicações Hibridas com Phonegap e HTML5
Aplicações Hibridas com Phonegap e HTML5
 
Git presentation to some coworkers some time ago
Git presentation to some coworkers some time agoGit presentation to some coworkers some time ago
Git presentation to some coworkers some time ago
 
Intrudução ao Behavior Driven Development (BDD) com Ruby on Rails
Intrudução ao Behavior Driven Development (BDD) com Ruby on RailsIntrudução ao Behavior Driven Development (BDD) com Ruby on Rails
Intrudução ao Behavior Driven Development (BDD) com Ruby on Rails
 

Interfaces ricas com Rails e React.JS @ Rubyconf 2015

  • 1. Globalcode  – Open4education Interfaces  ricas  com  Rails  e  React.JS Rodrigo  Urubatan @urubatan
  • 2. Globalcode  – Open4education Quem? Programador desde 1997 Trabalha com Ruby na Brightwire Escreveu "Ruby On Rails: Desenvolvimento fácil e Rápido de aplicações web” Já trabalhou com diversas linguagens (C, C++, Delphi, PHP, ASP, ColdFusion, VisualBasic, C#, Python, Ruby, Assembly, …) Apaixonado por Agile e atualmente por trabalho remoto Patinador, Corredor, Ciclista e agora resolveu aprender Karate :D Pai de um Guri de 6 anos http://urubatan.com.br - Anda meio abandonado mas vai voltar http://sobrecodigo.com - idem
  • 3. Globalcode  – Open4education Objetivo (O blog mais feio do mundo!)
  • 4. Globalcode  – Open4education Objetivo Usar o Rails como backend da aplicação Toda a interação com o usuário será implementada no cliente Validações e regras de negócio serão implementadas no servidor (sim, eu poderia usar Sinatra mas sou preguiçoso)
  • 5. Globalcode  – Open4education Cuidado! Nesta palestra vamos falar de uma SPA (Single Page Application) Isto tem vantagens e desvantagens Melhora muito a interação com o usuário sem duplicação de código, já que o código de renderização fica todo no JS Piora muito a indexação da sua aplicação por buscadores (adeus SEO - ou não…)
  • 6. Globalcode  – Open4education Criando a aplicação Uma aplicação padrão Rails (rails new …) Gemfile updates gem 'backbone-on-rails' gem 'react-rails', github: 'reactjs/react-rails', ref: 'master' Environment update (development.rb para começar) config.react.variant = :development config.react.addons = true # defaults to false config.react.server_renderer = React::ServerRendering::SprocketsRenderer bundle install rails g react:install
  • 7. Globalcode  – Open4education Componentes Componentes javascript vão ficar em app/assets/javascript/components Backbone.js vai facilitar a comunicação cliente/servidor arquivos .js.jsx tem uma facilidade extra, são compilados pelo react-rails via asset pipeline e permitem adicionar HTML inline
  • 8. Globalcode  – Open4education Cadastrando um Usuário rails g scaffold post title:string slug:text content:text Apagar todas as views do post exceto index.html.erb Apagar todo o código de index.html.erb e mudar para: <%= react_component('Blog', {collection: @posts, item: @post}) %>
  • 9. Globalcode  – Open4education Alterações no controller Fazer todos os métodos retornarem json, mais ou menos assim (não é a melhor tecnica, mas boa o suficiente para o exemplo) class PostsController < ApplicationController before_action :set_post, only: [:show, :edit, :update, :destroy] def index @posts = Post.all end def show @posts = Post.all render :index end # GET /posts/new def new @post = Post.new render :index end # POST /posts # POST /posts.json def create @post = Post.new(post_params) respond_to do |format| if @post.save format.json { render json: @post, status: :created, location: @post } else format.json { render json: @post.errors, status: :unprocessable_entity } end end end #  PATCH/PUT  /posts/1 #  PATCH/PUT  /posts/1.json def update respond_to do  |format| if  @post.update(post_params) format.json {  render  json:  @user,  status:  :ok,  location:  @post  } else format.json {  render  json:  @post.errors,  status:  :unprocessable_entity } end end end #  DELETE  /posts/1 #  DELETE  /posts/1.json def destroy @post.destroy respond_to do  |format| format.json {  head  :no_content } end end private #  Use  callbacks  to  share  common  setup  or  constraints  between  actions. def set_post @post  =  Post.find_by(slug:  params[:id])  ||  Post.find(params[:id]) end #  Never  trust  parameters  from  the  scary  internet,  only  allow  the  white  list  through. def post_params params.require(:post).permit(:title,  :slug,  :content) end end
  • 10. Globalcode  – Open4education Agora mãos a obra já temos uma “API" em Rails, poderíamos ter o código em Sinatra que seria mais leve, mas eu gosto do asset pipeline e assim fica mais fácil para um iniciante Falta criar os componentes backbone para acessar o backend Criar os componentes react para a UI
  • 11. Globalcode  – Open4education backbone.js app/assets/javascripts/collections/posts.js var Posts = Backbone.Collection.extend({ model: Post, url: '/posts' }); app/assets/jaascripts/models/post.js var Post = Backbone.Model.extend({ });
  • 12. Globalcode  – Open4education blog.js.jsx var Blog = React.createClass({ getInitialState: function(){ var posts = null; var post = null; if (this.props.collection) { posts = new Posts(this.props.collection); } else { posts = new Posts(); posts.fetch({success:function(data){ this.forceUpdate(); }.bind(this)}); } if (this.props.item) { post = new Post(this.props.item); } else { post = new Post(); } return { collection: posts, model: post, editing: false }; }, componentDidMount:function(){ this.router = new PostRouter({blog: this}); Backbone.history.start({pushState: true, root: "/"}); }, editModel:function(model){ this.setState({ model: model, editing: true }) }, viewModel:function(model){ this.setState({ model:  model, editing:  false });; this.router.navigate("/posts/"  +  model.get("slug"),  {trigger:  true});; }, newModel:function(){ this.setState({ model:  new  Post(), editing:  true }) }, render:  function   ()  { if(this.state.editing)  { return  (<div> <div  id="blogList"> <PostList collection={this.state.collection}  viewModel={this.viewModel}   newModel={this.newModel}/> </div> <div  id="blogPost"> <PostForm collection={this.state.collection}  model={this.state.model}   viewModel={this.viewModel}/> </div> </div>);; }else{ return  (<div> <div  id="blogList"> <PostList collection={this.state.collection}  viewModel={this.viewModel}   newModel={this.newModel}/> </div> <div  id="blogPost"> <PostView model={this.state.model}  editModel={this.editModel}/> </div> </div>);; } } });;
  • 13. Globalcode  – Open4education post_list.js.jsx var PostList = React.createClass({ componentDidMount: function () { this.props.collection.on("change", function () { this.forceUpdate() }, this); this.props.collection.on("reset", function () { this.forceUpdate() }, this); }, componentWillUnmount: function () { this.props.collection.off(null, null, this); }, render: function () { var users = this.props.collection.map(function (model) { var viewModel = function () { this.props.viewModel(model); }; return ( <tr key={model.get("id")}> <td><a href="#" onClick={viewModel.bind(this)}>{model.get("title")}</a></td> <td>{new Date(model.get("created_at")).toDateString()}</td> </tr> ); }.bind(this)); return ( <div> <table className="post-list"> <thead> <tr> <th>Post</th> <th>Published</th> </tr> </thead> <tbody> {users} </tbody> </table> <hr/> <a href="#" onClick={this.props.newModel}>Create post</a> </div> ); } });
  • 14. Globalcode  – Open4education post_view.js.jsx var PostView = React.createClass({ editModel: function () { this.props.editModel(this.props.model); }, render: function () { var innerLines = null; if(this.props.model.get("content")) { innerLines=_.map(this.props.model.get("content").split("n"), function (txt, idx) { return <p key={idx}>{txt}</p> }); } return ( <div className="blog-post"> <h1><a href={this.props.model.get("slug")}>{this.props.model.get("title")}</a></h1> <div className="post-body"> {innerLines} </div> <hr/> <a href="#" onClick={this.editModel}>Edit post</a> </div> ); } });
  • 15. Globalcode  – Open4education post_form.js.jsx var PostForm = React.createClass({ saveModel: function () { if (this.props.model.get("id")) { this.props.model.save(); } else { this.props.collection.create(this.props.model); } this.props.viewModel(this.props.model) }, render: function () { return ( <div className="blog-post"> <InputWithLabel model={this.props.model} label="Title" name="title" type="text"/> <InputWithLabel model={this.props.model} label="Body" name="content" type="textarea"/> <div className="form-field"> <button onClick={this.saveModel}>Save</button> </div> </div> ); } });
  • 16. Globalcode  – Open4education input_with_label.js.jsx var InputWithLabel = React.createClass({ handleChange: function(event) { this.props.model.set(this.props.name,event.target.value) }, render: function() { return <div className="form-field"> <label htmlFor={this.props.name}>{this.props.label}</label> <div> <input id={this.props.name} type={this.props.type} name={this.props.name} ref="input" onChange={this.handleChange} value={this.props.model.get(this.props.name)}/> </div> </div>; } });
  • 17. Globalcode  – Open4education O que, quando, onde e por que? Muitas aplicações hoje em dia exigem um nível alto de interação com o usuário Implementar isto usando bibliotecas mais baixo nível é muito fácil de causar uma grande confusão do código (PHP alguem?) Componentização evita duplicação de código e facilita a organização
  • 19. Globalcode  – Open4education indexação? performance? renderização no servidor: <%= react_component('Blog', {collection: @posts, item: @post}, {prerender: true}) %> components.js //= require underscore //= require backbone //= require_tree ./models //= require_tree ./collections //= require_tree ./components
  • 20. Globalcode  – Open4education Mas é só isto? React-router Backbone.Router Flux - arquitetura JS usada pelo Facebook Pré processamento com Gulp (sintaxe do ECMAScript 6 suportada)