Code Monkey home page Code Monkey logo

argument-injector's Introduction

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);
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.