Transcript
Page 1: Building a JavaScript Module Framework at Gilt

Building a JavaScript Module Framework

at Gilt

Eric Shepherd@arkitrave

Page 2: Building a JavaScript Module Framework at Gilt

Whois?

‣ background‣ music | architecture |

web design | front end engineering‣ companies‣ fisher-price | condé nast | gilt groupe

Page 3: Building a JavaScript Module Framework at Gilt

Thank You!

‣ Nicholas Zakas‣ scalable javascript application

architecture presentation‣ jQuery Conference 2010‣ presentations on pubsub, templating,

yepnope, and js architecture

Page 4: Building a JavaScript Module Framework at Gilt

Why?

Page 5: Building a JavaScript Module Framework at Gilt

Why?

‣ code organization‣ small files + objects, each with a

defined role

Page 6: Building a JavaScript Module Framework at Gilt

Why?

‣ code organization‣ reduce points of dependency‣ modules communicate through only

one channel

Page 7: Building a JavaScript Module Framework at Gilt

Why?

‣ code organization‣ reduce points of dependency‣ unobtrusiveness for third-parties‣ other sites can include your code

without fear

Page 8: Building a JavaScript Module Framework at Gilt

Why?‣ code organization‣ reduce points of dependency‣ unobtrusiveness for third-parties‣ adaptability + extensibility‣ any piece of the application can be

switched out without affecting the rest of the application

Page 9: Building a JavaScript Module Framework at Gilt

Demands

Page 10: Building a JavaScript Module Framework at Gilt

Demands

‣ allows multiple unique module instances

Page 11: Building a JavaScript Module Framework at Gilt

Demands

‣ allows multiple unique module instances

‣ does not allow modules to talk to each other‣ freedom in clear boundaries

Page 12: Building a JavaScript Module Framework at Gilt

Demands‣ allows multiple unique module

instances‣ does not allow modules to talk to

each other‣ tightly restricts dom access (with a

few exceptions)‣ the page doesn’t need to worry

about being messed with

Page 13: Building a JavaScript Module Framework at Gilt

Demands‣ allows multiple unique module

instances‣ does not allow modules to talk to

each other‣ tightly restricts dom access (with a

few exceptions)‣ touches as little global code as

possible‣ ideally, only one global var

Page 14: Building a JavaScript Module Framework at Gilt

Components

Page 15: Building a JavaScript Module Framework at Gilt

Components

‣ Gilt.App‣ there is only one‣ manages all modules, provides one

sandbox to each module ‣ public api is register(), start(), stop(),

startAll(), stopAll(), view.register()

Page 16: Building a JavaScript Module Framework at Gilt

Components

‣ Gilt.App‣ Gilt.Sandbox‣ instantiable, one instance per module‣ provides pubsub, state management,

dom interaction, services, views

Page 17: Building a JavaScript Module Framework at Gilt

Components‣ Gilt.App‣ Gilt.Sandbox‣ module itself‣ implements a simple create() and

destroy() interface‣ it’s an overprotected child, with a

limited view of the world

Page 18: Building a JavaScript Module Framework at Gilt

Components

‣ Gilt.App‣ Gilt.Sandbox‣ module itself‣ …along with core code and helpers

Page 19: Building a JavaScript Module Framework at Gilt

Architecture

Page 20: Building a JavaScript Module Framework at Gilt

Flow‣ a module is a function, passed to

App.register() as a creator‣ on App.start(), a new Sandbox()

instance is passed to that creator, which returns public create and destroy methods, and…

‣ the new module instance’s create() is called

Page 21: Building a JavaScript Module Framework at Gilt

Architecture

Page 22: Building a JavaScript Module Framework at Gilt

Gilt.App

Page 23: Building a JavaScript Module Framework at Gilt

Gilt.App

Page 24: Building a JavaScript Module Framework at Gilt

Architecture

Page 25: Building a JavaScript Module Framework at Gilt

Gilt.Sandbox

Page 26: Building a JavaScript Module Framework at Gilt

Gilt.Sandbox

Page 27: Building a JavaScript Module Framework at Gilt

Architecture

Page 28: Building a JavaScript Module Framework at Gilt

Module

Page 29: Building a JavaScript Module Framework at Gilt

Module

Page 30: Building a JavaScript Module Framework at Gilt

Module View (opt.)

Page 31: Building a JavaScript Module Framework at Gilt

Module View (opt.)

Page 32: Building a JavaScript Module Framework at Gilt

Architecture

Page 33: Building a JavaScript Module Framework at Gilt

The Hoff Helpers‣ jQuery‣ underscore.js‣ handlebars.js‣ yepnope.js‣ &c…but modules can’t know

anything about these (jQuery as a partial exception due to its plugin architecture)

Page 34: Building a JavaScript Module Framework at Gilt

Architecture

Page 35: Building a JavaScript Module Framework at Gilt

Wiring It Up

‣ on your own site‣ page controller

‣ on a third-party site‣ bootstrap as js loader and controller

Page 36: Building a JavaScript Module Framework at Gilt

WTF

Page 37: Building a JavaScript Module Framework at Gilt

A Better Bootstrap

‣ don’t be a jerk – think of the children‣ protect the global scope – you’re in

someone else’s house!‣ make it easy to inline or inject the

script dynamically‣ keep the payload light – it’s like

spending someone else’s money

Page 38: Building a JavaScript Module Framework at Gilt

A Better Bootstrap

Page 39: Building a JavaScript Module Framework at Gilt

…Which Returns…

Page 40: Building a JavaScript Module Framework at Gilt

Server-GeneratedClosure

Page 41: Building a JavaScript Module Framework at Gilt

Yep. Nope.

Page 42: Building a JavaScript Module Framework at Gilt

Watch Out For…

Page 43: Building a JavaScript Module Framework at Gilt

Watch Out For…

‣ make sure your bootstrap is RESTful

Page 44: Building a JavaScript Module Framework at Gilt

Watch Out For…‣ make sure your bootstrap is RESTful‣ css‣ conflicts are a bitch‣ and you can’t use id selectors‣ !important is your only hope‣ jquery.important can help, but it’s the

roach motel, and animations are out

Page 45: Building a JavaScript Module Framework at Gilt

Watch Out For…

‣ make sure your bootstrap is RESTful‣ css‣ jQuery‣ plugin convention requires a globally-

accessible jQuery object‣ so noConflict(true) will work only if

you don’t need plugins‣ use sandbox.getContainer().find()

Page 46: Building a JavaScript Module Framework at Gilt

Watch Out For…

‣ make sure your bootstrap is RESTful‣ css‣ jQuery‣ cross-domain issues‣ make sure your feeds are jsonp

Page 47: Building a JavaScript Module Framework at Gilt

Watch Out For…‣ make sure your bootstrap is RESTful‣ css‣ jQuery‣ cross-domain issues‣ assumptions of shared code‣ included code can later introduce

dependencies unavailable on a third-party site, hard to test

Page 48: Building a JavaScript Module Framework at Gilt

You Can Do Anything

‣ anything at all‣ the only limit is yourself‣ …yes…

Page 49: Building a JavaScript Module Framework at Gilt

Questions?

‣ Eric Shepherd‣ [email protected]‣ @arkitrave


Top Related