Built to Last




Building scalable front-end application architectures
with business logic.
Dan Lynch
Engineer & Artist




            questions/thoughts?
                                  @skatalynch
The Web
Building Things




                  1999, AutoCAD 10
I like helping other people
build
SideFX
city builder interface




2006
Building things that build
things

Vim3D

vim interface to building 3D
famo.us




our biz dev built this
app on the first famo.us
framework
interface for building
interactive textbooks




           mathapedia
building tools for people
Modularity is the degree to which   top down
     a system’s components may
     be separated and recombined.
     (wikipedia)

                                            ?
     Abstraction is the process of
     formulating generalized ideas or
     concepts by extracting common
     qualities from specific examples    bottom up
     (the free dictionaty)

10
Modularity                     top down
     •	 odules
      m
     •	 xtensions
      e


                    large-scale rapid development

     Abstraction
     •	 outing
      r
     •	 ages
      p                             bottom up
     •	 ata/APIs
      d
11
Why Modularity?
Adding New Code
Most time you spend is maintaining
code, not writing new code
Intuitive
Co-workers depend on your code to
make sense
Extendability
If features change, it’s
easy to extend or swap
parts
Debug/Test
It’s easy to test individual modules
The Mythical Man Month
Frederick Brooks Jr.




“Men and months are interchangeable commodi-
ties only when a task can be partitioned among many
workers with no communication among them”
Complex Communication
Spaghetti Code
•	 specific to application
•	 not reusable
•	 the app is the testing framework
Perfectly Partitionable
Modules
•	interdependent
•	little knowledge about the application
•	it is the sum of the modules that makes up the application
Module: Each of a set of standardized parts
or independent units that can be used to con-
struct a more complex structure (google definition)
Various Module Definitions +
Terminolgies
•	 JS module pattern
•	 AMD modules
•	 Web application modules
JavaScript Module Pattern




private and public encapsulation using closures
AMD Modules
Web Application Modules


   HTML + CSS + JS


       (components)
Application Modules

       model


        view


      controller
Application Framework

“An Application Framework is like a playground
                 for your code”

              - Nicholas Zakas
Application Framework
module      module     module



module      sandbox    module


extension             extension
             core
extension             extension
Where to start

backbone boilerplate
	 modular + build for production

aura
	 modular + widgets + message passing
Sandbox
facade
“The programmer is most effective if shielded
from, rather than exposed to the details of
construction of system parts other than his
own“

-Frederick Brooks Jr.
each module has a sandbox
    module           module           module



    module           sandbox          module


    extension                        extension
                       core
    extension                        extension

   This promotes loose coupling so you can change
           modules without affecting others
each module has a sandbox
sandbox exposes functionality
     module   module    module




              sandbox




               core
sandbox exposes functionality
application core
module      module    module



module      sandbox   module


extension             extension
             core
extension             extension
module registration, lifecycle,
and intercommunication
application core is responsible for:

message passing



module life-cycle
application core - the mediator




•	communication hub & centralized controller
•	intermediary between interdependent modules
application core - the mediator
                       only N channels!


              module



              module                      mediator




              module



modules use transitive communication through mediator
publish




photo gallery module
publish

             “get-fb-photos”




Gallery Module
“get-fb-photos”




mediator
subscribe
“get-fb-photos”




            FB Integration Module
subscribe




fb integration module
photo gallery module
module life-cycle
build for
extension
extending the core
module      module    module



module      sandbox   module


extension             extension
             core
extension             extension
Where is my layout?
Where do my routes go?
Where does my data live?
Decouple Your Web App
Navigation
 URLs/routes/controllers

Data
 data/models/collections

Pages
 layouts/views/templates
layout extension




enable abstraction of navigation paradigms
route:books

router



         route:books

module                  mediator
         render:books




         render:books
layout
controller
layout
module group extension

If a group of modules
represents an
application, what if we
can manage groups to
run multiple applications
or portals within a single
page app?
module group extension
no
                               change group  v




        route
                         yes
route           group?          controller
module group extension
change group

               swap layout

                       afterRender()


               start modules

  $.when.apply($, promises).done
Protecting Resources/Modules
typical backend

                 flow




                             yes
route         middleware           controller
                        no


    middleware can protect resources
can the front-end JS function like the backend?



           route             controller




   (typical Backbone routing with no FSM)
Finite State Machine
•	 events
•	 states
•	 transitions
•	 actions
fail()

                                               v




                       no




           route
                            yes
  route             fsm           controller


Backbone routing with FSM
v




                                      swap layout
        fail()



            no                 no

                                      start modules
                 yes
route   FSM                 group?



                                yes
                                         enter()




                       controller     resetRoutes()
router extension
•	manage routes, delegate route callbacks to FSM
module groups extension
•	modify core to handle groups with async/dfd
layout extension
•	render views using message passing
fsm extension
•	manage state (group)
•	if routes pass, send allow controllers to callback
data extension
•	manage model/collection cache
what we covered
 how to take core components of a
web application (routes, navigation,
layouts, etc) and divy them up into
  modules/extensions so that the
  application can grow efficiently;
   without the need to refactor.

      bottom up + top down!
node.js	                 tech to use and checkout
	 end-to-end JS!
backbone boilerplate	
	 great for build + gettings started
aura	
	 widgets and module lifecycle
backbone layout manager	
	 views/layouts
async.js
	 asyncronous flow control
grunt.js	
	 build
require.js
	 module loading
r.js
   optimizer
props, credit, and people to checkout

           Tim Branyen
           Addy Osmoni
          Nicholas Zakas
Thank You!




             @skatalynch

Built to Last