GithubHelp home page GithubHelp logo

candi's Introduction

draft - more features are available I just need to find time to update the documentation. I do this for fun :)

canDI

A simple dependency injection and object creation javascript library (with no dependencies!!)

candi.provider

provider is the main interface used to create objects, and implicitly define and inject dependencies into an object. It also automatically registers your objects as dependencies to be injected into other objects created with provider.

You can create...

Singletons

candi.provider.singleton(String:name, Function:factory, Object:scope)

  • name: The name of the newly created singleton.
  • factory: The function that will be used to create your singleton.
  • scope: The value of this within the factory. If undefined then scope will be a new object {}.

singleton will create and return the newly created singleton with resolved dependencies injected and ready to use. It will also automatically define your new singleton as a new dependency that can then be referenced and injected into other objects created with provider.

Example:

candi.provider.singleton('_util', function() { 
  return {
    isString: function(obj) { return typeof obj === 'string'; }
  };
});

// singleton also returns your newly create singleton object, dependencies injected
// and ready to use.
var robot = candi.provider.singleton('robot', function(_util) { 
  return {
    greet: function(name) {
      if(!_util.isString(name)) throw new Error('Invalid argument. 'name' must be a string.);
      return 'hello ' + name + '!!';
    }
  };
});

Instances

candi.provider.instance(String:name, Function:factory, Object:scope)

  • name: The name of the newly created singleton.
  • factory: Your instance constructor.
  • scope: The value of this within the factory. If undefined then scope will be a new object {}.

instance will create and return a function that defines your instance definition. It will inject not only dependencies but also other arguments passed at instance creation. It will also automatically register your new instance definition as a dependency that can be referenced and injected into other objects created with provider.

Example:

candi.provider.singleton('infoService', function(ajax) { 
  return {
    getTopSpeed: function(maker, model, year) { return ajax.get('topspeed', maker, model, year); }
  };
});

var Car = candi.provider.instance('Car', function(infoService, maker, model, year) {
  this.maker = maker;
  this.model = model;
  this.year = year
  this.topSpeed = infoService.getTopSpeed(maker, model, year);
});

var myCar = new Car('honda', 'civic', 2004);

Variables

candi.provider.variable(String:name, Object:value)

Example:

candi.provider.variable('myVariable', 'hello');
// OR
candi.provider.variable('days', ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']);

candi's People

Contributors

bflemi3 avatar

Watchers

 avatar James Cloos avatar Jay Lockwood avatar

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.