guardian/frontend — pull request #2775 by jamesgorrie (2014-01-15 13:34:24+00:00)
Title: Context is king

Whilst doing a little tidy up of the discussion modules, and having a chat to @phamann, a thought of having the context as a dependancy made a lot of sense. This way we aren't passing around a variable like a hot potato.

## Why?
Instead of doing DI through passing arguments around, we use RequireJS.
Breaking our common utilities down into bite size surprises was a step in this direction (e.g. the mediator).
This is just following that pattern.

## Show me the money
```javascript
// Module man.js
define(['common/utlis/context'], function(context) {
var Man = function() {
var elem = document.createElement('blink');
context().appendChild(elem);
}
});

// Parent...
define(['common/utils/context', 'man'], function(context, Man) {
var freeMan = new Man(); // renders in body
context.set(document.getElementById('concreteShoes'));
var trappedMan = new Man(); // renders in element
});

## Notes
* Context is functional and should be assigned to variable in scope as it might changed later in the chain of events.

![KING](http://gifs.gifbin.com/250yu273swsw.gif)