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
85
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
170
Impressoras 3D
ricardobeat
0
130
Other Decks in Programming
See All in Programming
Git Rebase
bkuhlmann
11
1.6k
Site Reliability Engineering for GMO
pyama86
7
1k
코틀린으로 멀티플랫폼 만들기
pangmoo
0
150
1BRC--Nerd Sniping the Java Community
gunnarmorling
0
340
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
640
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
エンターテイメント業界で利用されるAWS
demuyan
0
210
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
Amazon SQSコンシューマー疎結合への旅 - 出張! #DevelopersIO IT技術ブログの中の人が語る勉強会 #3
quiver
0
230
PostmanでAPIの動作確認が楽になった話
h455h1
0
160
大規模Reactアプリのリアーキテクチャ~8万行のTanStack Query移行の軌跡~
kj455
4
940
"config" ってなんだ? / What is "config"?
okashoi
0
240
Featured
See All Featured
A Philosophy of Restraint
colly
197
16k
Being A Developer After 40
akosma
57
580k
The Cult of Friendly URLs
andyhume
74
5.7k
Docker and Python
trallard
34
2.7k
Principles of Awesome APIs and How to Build Them.
keavy
121
16k
Embracing the Ebb and Flow
colly
80
4.1k
Making the Leap to Tech Lead
cromwellryan
124
8.5k
Fireside Chat
paigeccino
21
2.6k
Designing on Purpose - Digital PM Summit 2013
jponch
110
6.5k
Automating Front-end Workflow
addyosmani
1356
200k
Optimizing for Happiness
mojombo
370
69k
What the flash - Photography Introduction
edds
64
11k
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!