2.1.1 • Published 9 years ago

argument-injector v2.1.1

Weekly downloads
20
License
-
Repository
-
Last release
9 years ago

Build Status Coverage Status

argument-injector is a function argument injector for JavaScript. it allows any function to be "bound" to an injector object, which hijacks the function call and injects arguments when the function is executed.

usage

to get started, you'll first need to create a new instance of an Injector:

var Injector = require('argument-injector'),
    injector = new Injector();

this injector object will have three methods you care about: #register, #bind, and #trigger

register

this method allows you to "save" a variable in the injector object. there are the variables that are available during a function call. this is the signature of the method: Injector register(String name, * variable)

injector.register('myService', {
    isActive: function () {
        return true;
    }
});
bind

the bind method is what ties a function to your injector. this works by taking your function, and returning a copy which checks for arguments passed and arguments available in the injector when the function is invoked. this is the signature of the method: Function bind(Function func, Object scope)

var myFunction = injector.bind(function (myService) {
    return myService.isActive() ? 'you are active' : 'you are not active';
});

you can call myFunction the same way you would any other function, with the exception that you do not need to pass it a myService variable.

bind will parse the signature of the function you gave it and generate a list of the required arguments. once the function is invoked, this list of arguments is checked against the registered variables and passed to the real function.

trigger

there's also a trigger method, which works just like bind, except it will invoke the funciton immediatelly and return that call's return value instead of a bound function. this is the signature of the method: * trigger(Function func, Object scope)

injected.register('name', 'Marcos');
injector.trigger(function (name) {
    // will output 'Marcos'
    console.log(name);
});
2.1.1

9 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.0

10 years ago