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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
XHTMLが残したもの
yosuke_furukawa
PRO
2
670
ハーネス設計入門 〜プロンプト、コンテキストの次〜
kinopeee
55
37k
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
630
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
240
AIとGame Jamで、ゲームを完成させた話
takahirosaeki
0
120
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
280
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
130
GKE で Pod の見方を変えたら、スケールアウト時の挙動を真に捉えられた話
stkk
0
110
AI に Inclusive UI を書かせよう — Design Rules Skill で Compose UI を作り直す
theoriatec2024
1
470
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
140
市販E-Readerを乗っ取れ 〜Embedded Swiftで電子ペーパーガジェットを制御する〜
trickart
0
160
AIを上手に使っていこうとしたら越境せざるを得なくなった話 〜実践1年で見えた境界を越えなければならない理由と進め方〜 / Crossing borders with AI
tomoyakitaura
4
1.1k
Featured
See All Featured
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
270
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
RailsConf 2023
tenderlove
30
1.5k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
400
Faster Mobile Websites
deanohume
310
32k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
520
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
320
Embracing the Ebb and Flow
colly
88
5.2k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
470
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
410
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Building the Perfect Custom Keyboard
takai
2
870
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