18F/mirage — pull request #125 by theresaanna (2014-09-16 23:58:08+00:00)
Title: JS Refactoring

### Please don't merge yet!
I'm still having problems getting contract data into my local and haven't been able to test that feature yet.

#### The goal of this PR
- To JSify syntax and add JS conventions
- To provide structure/scoping
- To make more full use of jQuery
... all for ease of maintenance and continued development (hopefully!)

#### Major changes
- Introduced http://thejacklawson.com/Mediator.js/, a small event library. Actions that take place across objects (pseudo-classes) happen because of a series of custom events that they subscribe to or publish (trigger) at key times. This is the replacement for much of the connectivity that having all functions in the global scope provided before.
- Put things in strict mode: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
- Structure...

#### Code Structure
##### InputHandler
- populates/updates checkboxes and drop down
- triggers event changes starting at user input to a field
- is the thing to ask for current setasides and naics code

##### RequestsManager
- listens for events where data should be loaded and loads the data
- assembles query data used around the app for context and new requests

##### URLManager
- generates query strings
- updates pushState and reloads to pool page accordingly
- helper methods to get data out of the url

##### LayoutManager
- listens for events that mean data should be rendered
- renders out content

#### Changes since code walkthrough
- ResultsManager became RequestsManager
- Introduced template-specifc extensions of RequestsManager and LayoutManager for content-specific render and data loading needs
- requests-manager-vendor.js
- requests-manager-listings.js
- layout-manager-vendor.js
- layout-manager-listings.js

###### Nomenclature:
Listings = results lists, either multiple or a single pool
Vendor = single vendor page

We should rename these, and any other values in the code, to whatever makes more sense to the rest of the team. I developed my own way of thinking about these things that probably don't fully match yours.

#### Tips
- To get to know the runtime flow, look at each object's init method. Check the events it subscribes to.
- grep for 'Events.publish' or the event string that you're interested in.
- Files that extend objects, like requests-manager-*.js and layout-manager-*.js, also extend the object's init method. Though it looks like trickery, what happens is that the original init method is copied, a newly defined one is called and within it the original init method is called as well.
- If you're not familiar, the bind method looks trickier than it is: http://www.smashingmagazine.com/2014/01/23/understanding-javascript-function-prototype-bind/, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind