summaryrefslogtreecommitdiff
path: root/README.md
blob: 5b11d47c5b25d8ca568bb6f910c479a3c55bb87a (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
* Scorpion

** What is it?

Scorpion is a simple dependency injection framework for
Javascript. It provides the ability to both construct and destroy
dependencies, as well as injecting a wide variety of things
(extensible via "plugins").

** How do I use it?

Just include `scorpion.js` or `scorpion.min.js` in your page to get
started.

Then create an injector with some plugins (this one will use the
standard "value plugin", which behaves similarly to other dependency
injection frameworks):

    :::javascript
    var injector = new Scorpion([new Scorpion.ValuePlugin()]);
    injector
        .register("DepA", function() {
            return "Dependency A";
        })
        .register("DepB", ["DepA", function(DepA) {
            return ["Dependency B", DepA];
        }]);

Now we can create our dependencies:

    :::javascript
    // we can just get a value (in a promise)
    injector.get("DepB").then(function(value) {
        console.log("Value returned:", value);
    }, console.error.bind(console));

    // or we can invoke a function (with the result returned in a promise)
    injector.invoke(["DepA", "DepB", function(DepA, DepB) {
        return {A: DepA, B: DepB};
    }]).then(function(value) {
        console.log("Value returned:", value);
    }, console.error.bind(console));

Dependencies are constructed asynchronously, which lets us also create
different types of plugins, like the HTTPPlugin which comes with
Scorpion:

    :::javascript
    var injector = new Scorpion([
        Scorpion.prefixPlugin("http!", new Scorpion.HTTPPlugin()),
        new Scorpion.ValuePlugin()
    ]);
    injector.register("LoginDOM", ["$", "http!/partials/login.html", function($, login) [
        return $(login);
    }]);
    injector.get("LoginDOM").then(function(value) {
        console.log("dom:", value);
    });