Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Construindo aplicações web com Backbone e Node.js
Search
Ricardo Tomasi
March 26, 2013
Programming
3
94
Construindo aplicações web com Backbone e Node.js
Palestra apresentada no RSJS 2013 (
http://rsjs.org
)
Ricardo Tomasi
March 26, 2013
Tweet
Share
More Decks by Ricardo Tomasi
See All by Ricardo Tomasi
Introdução a CoffeeScript
ricardobeat
1
180
Impressoras 3D
ricardobeat
0
130
Other Decks in Programming
See All in Programming
Select API from Kotlin Coroutine
jmatsu
1
190
設計やレビューに悩んでいるPHPerに贈る、クリーンなオブジェクト設計の指針たち
panda_program
6
1.4k
5つのアンチパターンから学ぶLT設計
narihara
1
110
ASP.NETアプリケーションのモダナイズ インフラ編
tomokusaba
1
410
生成AIで日々のエラー調査を進めたい
yuyaabo
0
650
来たるべき 8.0 に備えて React 19 新機能と React Router 固有機能の取捨選択とすり合わせを考える
oukayuka
2
850
LT 2025-06-30: プロダクトエンジニアの役割
yamamotok
0
410
deno-redisの紹介とJSRパッケージの運用について (toranoana.deno #21)
uki00a
0
150
Enterprise Web App. Development (2): Version Control Tool Training Ver. 5.1
knakagawa
1
120
0626 Findy Product Manager LT Night_高田スライド_speaker deck用
mana_takada
0
100
GraphRAGの仕組みまるわかり
tosuri13
7
480
「ElixirでIoT!!」のこれまでとこれから
takasehideki
0
370
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
234
140k
The Language of Interfaces
destraynor
158
25k
Code Review Best Practice
trishagee
68
18k
Navigating Team Friction
lara
187
15k
Thoughts on Productivity
jonyablonski
69
4.7k
Docker and Python
trallard
44
3.4k
A Modern Web Designer's Workflow
chriscoyier
694
190k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
490
Bash Introduction
62gerente
614
210k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Designing for humans not robots
tammielis
253
25k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
670
Transcript
@ricardobeat github.com/ricardobeat
NODE.JS BACKBONE + + event emitters + mvc + HTML5
+ LESS + COFFEESCRIPT + EXPRESS + CHAOS THEORY + IMPOSTO DE RENDA + CERVEJA ARTESANAL
EVENT EMITTERS (OBSERVER PATTERN)
None
EVENT BUS
EVENT BUS bus.on('hello', function(obj){ say('hello ' + obj.name) }) {
"hello": [Function] }
EVENT BUS { "hello": [Function] } bus.emit('hello', { name: 'robot'
}) hello robot
EVENT EMITTERS LOOSE COUPLING Fácil de testar escalável MODERNO E
BONITO
EVENT BUS
PUB-SUB
var robot = _.extend({}, Backbone.Events) robot.on('hello', ...) this.emit('hello', ...) hello
robot
BACKBONE
VIEW MODEL COLLECTION ROUTER
VIEW MODEL COLLECTION ROUTER EVENTOS REST JSON
None
INICIANDO UM APP COM BACKBONE
MODEL
var Conference = Backbone.Model.extend({ initialize: function(){ console.log('New conference!') }, defaults:
{ name: 'Default title', year: 2013 } }) MODEL
var Conference = _.extend({ initialize: function(){ console.log('New conference!') }, defaults:
{ name: 'Default title', year: 2013, city: 'São Paulo' } }, Backbone.Model) MODEL
// Criando uma nova conferência var RSJS = new Conference({
name: 'RSJS', year: 2013, city: 'Porto Alegre' }) // year === 2013 var year = RSJS.get('year') MODEL
// Alterando atributos RSJS.set({ name: "SurvivorConf" , year: 2022 })
RSJS.save() MODEL
var Conference = Backbone.Model.extend({ ... urlRoot: '/conferences' }) RSJS.save() ->
$.post('/conferences', { name: 'RSJS'... }) RSJS.id // => 1 MODEL
-> $.ajax({ url: '/conferences/1' , method: 'PUT' , data: {
year: 3000, ... } ... }) RSJS.set({ year: 3000 }) RSJS.save() MODEL
MODEL // carregar dados do server RSJS.fetch() // sends an
HTTP delete to / conferences/1 RSJS.destroy() // retorna todos os atributos RSJS.toJSON() eventos: 'change', 'change:attr', 'sync', 'error', 'destroy' ...
VIEW
var ConferenceView = Backbone.View.extend({ tagName: 'div', className: 'conference', render: function
() { // código para renderizar o html } }) VIEW
var ConferenceView = Backbone.View.extend({ tagName: 'div', className: 'conference', render: function
() { var name = this.model.get('name'), description = this.model.get('description') this.el.append( $('<h2>').addClass('title').text(name), $('<p>').text(description) ) } }) VIEW
var ConferenceView = Backbone.View.extend({ tagName: 'div', className: 'conference', template: _.template(conferenceTemplate),
render: function () { this.el.html(this.template(this.model.toJSON())) } }) VIEW
// Nova view usando o model RSJS var view =
new ConferenceView({ model: RSJS }) VIEW
VIEW var ConferenceView = Backbone.View.extend({ ... initialize: function (options){ this.model
= new Conference({ id: options.id }) this.model.fetch() this.model.on('sync', this.render) } render: function () { // render template } }) var view = new ConferenceView({ id: 123 })
VIEW var ConferenceView = Backbone.View.extend({ initialize: function (options) { this.model.on('change',
this.render) }, events: { 'click .subscribe': 'subscribe' }, subscribe: function(){ this.model.set('subscribed', true) } })
COLLECTION
COLLECTION var Calendar = Backbone.Collection.extend({ model: Conference, url: '/conferences' })
COLLECTION // instancia o Model automaticamente Calendar.add({ name: 'RSJS', year:
2012 }) Calendar.add(RSJS) Calendar.add(new Conference({ name: 'RSJS', year: 2013 }))
COLLECTION var events = new Calendar events.url = '/events' events.fetch()
ROUTER
ROUTER var CalendarRouter = Backbone.Router.extend({ routes: { "event/:id": "event", //
#event/123 "*path": "eventList" // catch-all }, event: function() { // render event view }, eventList: function(query, page) { // render event list } })
ROUTER Backbone.history.start({ pushState: true })
None
NODE + BACKBONE MESMA LINGUAGEM EVENT LOOP UNDERSCORE BACKBONE! JSON
NODE + BACKBONE var express = require('express') , app =
express() , db = new ImaginaryDB('127.0.0.1', 'root', '123456') app.configure -> ... app.use express.bodyParser() app.use app.router app.get('/', function (req, res) { res.render('index') })
NODE + BACKBONE app.get('/events', function(req, res) { db.getAll(function(err, events){ res.json(events)
}) }) var events = new CalendarCollection() events.url = '/events' events.fetch()
NODE + BACKBONE app.get('/event/:id', function(req, res) { db.getById(req.params.id, function(err, event){
res.json(event) }) }) var RSJS = new Conference({ id: 123 }) RSJS.fetch()
NODE + BACKBONE app.post('/event', function(req, res){ var event = new
db.Event(req.body) db.put(event, function(err, res){ res.json(res) }) }) var RSJS = new Conference({ name: 'RSJS', year: 2013 }) RSJS.save()
NODE + BACKBONE app.put('/event/:id', function(req, res) { db.update(req.params.id, req.body, function(err,
eve res.json(event) }) }) RSJS.set('year', 3000) RSJS.save()
BACKBONE.SYNC
IMPOSTO DE RENDA Já declarou?
None
Obrigado!