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
92
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
shadcn/uiを使ってReactでの開発を加速させよう!
lef237
0
280
ISUCON14感想戦で85万点まで頑張ってみた
ponyo877
1
490
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
1k
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
380
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
120
traP の部内 ISUCON とそれを支えるポータル / PISCON Portal
ikura_hamu
0
170
ecspresso, ecschedule, lambroll を PipeCDプラグインとして動かしてみた (プロトタイプ) / Running ecspresso, ecschedule, and lambroll as PipeCD Plugins (prototype)
tkikuc
2
140
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
3
560
React 19でお手軽にCSS-in-JSを自作する
yukukotani
5
550
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
1k
ドメインイベント増えすぎ問題
h0r15h0
2
550
menu基盤チームによるGoogle Cloudの活用事例~Application Integration, Cloud Tasks編~
yoshifumi_ishikura
0
150
Featured
See All Featured
Unsuck your backbone
ammeep
669
57k
Why Our Code Smells
bkeepers
PRO
335
57k
How STYLIGHT went responsive
nonsquared
96
5.3k
Gamification - CAS2011
davidbonilla
80
5.1k
Making the Leap to Tech Lead
cromwellryan
133
9k
Statistics for Hackers
jakevdp
797
220k
What's in a price? How to price your products and services
michaelherold
244
12k
A better future with KSS
kneath
238
17k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
Making Projects Easy
brettharned
116
6k
Designing for Performance
lara
604
68k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
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!