Skip to content

Instantly share code, notes, and snippets.

@unscriptable
Created May 11, 2011 23:00
Show Gist options
  • Save unscriptable/967587 to your computer and use it in GitHub Desktop.
Save unscriptable/967587 to your computer and use it in GitHub Desktop.
IOC/DI as an alternative to module swapping
// admittedly, dependency injection is better for non-integral dependencies.
// integral dependencies like coding constructs such as array iterators (forEach),
// type detectors (isArray, isFunction), etc are easier to work with as declared
// dependencies/modules. there are other ways to inject dependencies (e.g.
// constructor params). this is just how we're doing it.
// this example shows the injection of non-integral dependencies
define(function () {
return {
// templateEngine must be injected by controller or ioc container
templateEngine: null,
// template must be injected by controller or ioc container
template: null,
// do something with injected dependencies
render: function (domNode) {
var view;
view = this.templateEngine(this.template);
domNode.appendChild(view);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment