Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Limpando Seu Código JS Com O Padrão Pub/Sub
Search
Filipe Costa
December 13, 2014
Programming
200
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Limpando Seu Código JS Com O Padrão Pub/Sub
CEJS 2014
Code sample:
https://github.com/filipebarcos/cejs_pubsub
Filipe Costa
December 13, 2014
More Decks by Filipe Costa
See All by Filipe Costa
Lidando com Efeitos Colaterais com Redux Saga
filipebarcos
0
230
Pitch - Lidando com Efeitos Colaterais com Redux Saga
filipebarcos
0
410
Rust for Rubysts
filipebarcos
2
260
Tu trabalha em casa?? Que moleza hein!
filipebarcos
0
130
Rediscovering OOP in Rails World
filipebarcos
0
100
jQuery Bad Practices
filipebarcos
2
280
Intro to Ruby
filipebarcos
1
140
Other Decks in Programming
See All in Programming
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
470
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
310
カツオ、ご期待ください
suneo3476
0
150
FreeBSDでZabbixを動かす
kenkino
0
350
速習iPhone Duo対応
yuukiw00w
2
960
プロダクトコードからライブラリの境界を見つける
elmetal
PRO
0
100
モデルのリファクタリングが難しいと思ったら、そもそも複雑だったのはビジネス仕様だった ? / is-the-business-domain-the-real-complexity
hatsu38
0
440
AI時代のコードレビューは人に向けるな、仕組みに向けろ
texmeijin
5
3.5k
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
160
AWS CDKのカスタムリソースでContinuum(旧Security Agent)を実装した話 | Implementing Continuum (formerly Security Agent) via AWS CDK Custom Resource
akihisaikeda
2
130
Simple Storage Service(S3) is not simple
iwatsukayura
0
190
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
340
Featured
See All Featured
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
510
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
320
Become a Pro
speakerdeck
PRO
31
6.3k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Paper Plane
katiecoart
PRO
4
53k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.7k
So, you think you're a good person
axbom
PRO
2
2.2k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
290
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
710
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
Transcript
PUB/SUB -> CLEAN JS “Limpando Seu Código JS Com O
Padrão Pub/Sub"
FILIPE COSTA @filipebarcos
Stack Builders Estamos contratando!
DISCLAIMER
CLEAN CODE
TÁ NA MODA! Angular Ember Backbone React MVVM MVC MV******
CONTEXTO
• Comprar Passeio Turístico no Site • Página Única •
Descrição do produto • Horários (seletor de dia/hora) • Mapa • Seletor de quantidade • Resumo da Compra • Formulário de Cliente e Cartão • Total da Compra
None
MÓDULOS
Checkout.OrderSummary = function() { return { addItem: function(item) { //
Add item to summary // Update Order Total }; }; }; Checkout.CustomerForm = function() {};
O PROBLEMA
DEPENDÊNCIA
Checkout.OrderSummary = function() { var addItemToSummary = function(item) { var
orderSummary = document.getElementById('order-summary'); var rowSnippet = buildRow(item); orderSummary.appendChild(rowSnippet); }; var updateOrderTotal = function(item) { var orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + item.quantity * item.cost; }; return { addItem: function(item) { addItemToSummary(item); updateOrderTotal(item); }; }; };
Checkout.CustomerForm = function() { var fetchDeliveryCost = function(postalCode) { //fetch
from Post Office API }; var updateOrderTotal = function(cost) { var orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + cost; }; return { updateDeliveryCost: function(postalCode) { var cost = fetchDeliveryCost(postalCode); document.getElementById('delivery-cost').innerHTML = cost; updateOrderTotal(cost); }; }; };
SOLUÇÕES SOLUÇÃO
PUB/SUB
PUBlish e SUBscribe é um padrão de mensageria, onde os
“Publishers” enviam mensagens e os “Subscribers” escutam essas mensagens
None
PUBSUB.JS
None
PubSub.publish('my-channel', 'value'); PubSub.subscribe('my-channel', function(message, data) { console.log('this is what I
got'); console.log(data); });
DE VOLTA AO PROBLEMA
Checkout.CustomerForm = function() { var updateOrderTotal = function(cost) { var
orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + cost; }; }; Checkout.OrderSummary = function() { var updateOrderTotal = function(item) { var orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + item.quantity * item.cost; }; };
Checkout.OrderSummary = function() { var addItemToSummary = function(item) { var
orderSummary = document.getElementById('order-summary'); var rowSnippet = buildRow(item); orderSummary.appendChild(rowSnippet); }; return { addItem: function(item) { addItemToSummary(item); PubSub.publish('checkout.item', item); }; }; };
Checkout.CustomerForm = function() { var fetchDeliveryCost = function(postalCode) { //fetch
from Post Office API }; return { updateDeliveryCost: function(postalCode) { var cost = fetchDeliveryCost(postalCode); document.getElementById('delivery-cost').innerHTML = cost; PubSub.publish('checkout.delivery-cost', cost); }; }; };
Checkout.OrderTotal = function() { var updateOrderTotal = function(cost) { var
orderTotal = document.getElementById('order-total'), currentTotal = parseFloat(orderTotal.innerHTML); orderTotal.innerHTML = currentTotal + cost; }; PubSub.subscribe('checkout.item', function(msg, item) { updateOrderTotal(item.quantity * item.cost); }); PubSub.subscribe('checkout.delivery-cost', function(msg, deliveryCost) { updateOrderTotal(deliveryCost); }); };
\O/
CONCLUSÃO
OBRIGADO https://github.com/filipebarcos/cejs_pubsub