Last active
August 29, 2015 13:56
-
-
Save saaji/9238595 to your computer and use it in GitHub Desktop.
Tiny IOC in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
var o = $({}); | |
$.registerComponent = function(key, component) { | |
o[key] = component; | |
}; | |
$.unregisterComponent = function(key) { | |
o[key] = undefined; | |
}; | |
$.invoke = function() { | |
var args = Array.prototype.slice.call(arguments); | |
var key = args.shift(); | |
var func = args.shift(); | |
return o[key][func].apply(o[key], args); | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This tiny lib allows to inverse control of JS app by registering services and calling their functions. Unlike bigger implementations it does not bother with Dependency Inversion, although services potentially may call other services.
Common use cases for this lib are shopping carts, summary and/or status widgets, or service objects that collect data over steps of some wizard.
Here is the example of a simple shopping cart. It can be accessed directly and via events (using Tiny PubSub):