Tungsten.js
Building a Modular Framework
Who Are We
Andrew Rota
@andrewrota  
Matt DeGennaro
@thedeeg
Wayfair
Wayfair.com is an online destination for all things
home
Selection of more than seven million home items from 7,000
suppliers
Ecommerce company since 2002
Several large websites running a shared
codebase
PHP backend for customer­facing pages
2,000,000+ lines of code for custom e­commerce platform
2,000+ JavaScript modules
Why we did
For a large codebase, overhauls are rarely
possible a good idea
We needed a framework that we could upgrade
as technologies shifted
Let's Talk About Modularity
­ Edsger W. Dijkstra, Notes on Structured Programming, 1970
Large systems should be constructed out
of smaller components; each component
should be defined only by its interface and
not by its implementation
“
Systems Need to
Adapt
Lavaflow
Modularity
Requires Choices
Choices are your
Interfaces
Application Logic
DOM Manipulation
Templates
Backbone
virtual­dom
Mustache
Backbone
virtual­dom
Mustache
Familiar API for developers
Uses 'change' events to handle re­
renders
Delegates events using Tungsten's
Event interface
var AppView = View.extend({
  childViews: {
    'js‐new‐todo': NewItemView,
    'js‐todo‐item': TodoItemView
  },
  events: {
    'click .js‐toggle‐all': 'handleClickToggleAll'
    'click .js‐clear‐completed': 'handleClickClearCompleted
  }
});
Backbone
virtual­dom
Mustache
Handles taking the output from template and
updating the page
Creates patch operations from VirtualDOM
tree diffing and applies them to the DOM
[
  {
    "0": {
      "type": "PROPS",
      "vNode": {
        "tagName": "LI",
        "properties": {
          "className": "js‐todo‐item  "
        },
        "count": 13,
        "children": "[5 children]"
      },
      "patch": {
        "className": "js‐todo‐item  completed  "
      }
Backbone
virtual­dom
Mustache
Declarative descriptors of markup
Uses data to create the current state of
the app
Rendered on both Client and Server
<div id="main">
  <input id="toggle‐all" class="js‐toggle‐all" 
  <ul id="todo‐list">
    {{#todoItems}}
      {{> todo_item}}
    {{/todoItems}}
  </ul>
</div>
Templates?!
Separating
technologies not
concerns?
Different
environments have
different concerns.
Server choice should
not be dictated by
client­side choices
Implementations
FluxFlux
ReactReact
HandlebarsHandlebars
Implementations
BackboneBackbone
ReactReact
HtmlBarsHtmlBars
Implementations
FluxFlux
ReactReact
UnderscoreUnderscore
Implementations
FluxFlux
virtual­domvirtual­dom
HtmlBarsHtmlBars
Implementations
AmpersandAmpersand
ReactReact
JadeJade
Implementations
BackboneBackbone
Reactvirtual­dom
JadeJade
github.com/wayfair/tungstenjs

Tungsten.js: Building a Modular Framework