Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Limpando Seu Código JS Com O Padrão Pub/Sub

Filipe Costa
December 13, 2014

Limpando Seu Código JS Com O Padrão Pub/Sub

Filipe Costa

December 13, 2014
Tweet

More Decks by Filipe Costa

Other Decks in Programming

Transcript

  1. • 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
  2. Checkout.OrderSummary = function() { return { addItem: function(item) { //

    Add item to summary // Update Order Total }; }; }; Checkout.CustomerForm = function() {};
  3. 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); }; }; };
  4. 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); }; }; };
  5. PUBlish e SUBscribe é um padrão de mensageria, onde os

    “Publishers” enviam mensagens e os “Subscribers” escutam essas mensagens
  6. 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; }; };
  7. 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); }; }; };
  8. 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); }; }; };
  9. 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); }); };
  10. \O/