npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pimple

v1.0.0

Published

Dependency Injection container for javascript

Downloads

42

Readme

PIMPLE.JS

version 2.0.3-alpha

NPM

Build Status

###WARNING : this is the documentation for branch 2.X, the API has been heavily refactored from version 1.X

Pimple is a Dependency injection container for javascript , compatible with all javascript enabled browsers.

see : http://en.wikipedia.org/wiki/Dependency_injection

author M.Paraiso , inspired by Pimple by Fabien Potencier : https://github.com/fabpot/Pimple

contact: [email protected]

status: beta

features:

  • AMD compatible

change log

  • 2.0.0 API in sync with PHP version branch 2.X please check : https://github.com/fabpot/Pimple for changes
  • 0.0.4 pimple can now be instanciated without new
  • 0.0.3 api changed for shared,protected and extended services , see README.md
  • 0.0.2 register method added

###Usage

Creating a container is a matter of instating the Pimple class

var container = new Pimple({foo:'foo'});

As many other dependency injection containers, Pimple is able to manage two different kind of data: services and parameters.

####Defining Parameters

// define some parameters
container.set('bar','bar');

####Defining Services

A service is an object that does something as part of a larger system. Examples of services: Database connection, templating engine, mailer. Almost any object could be a service.

Services are defined by anonymous functions that return an instance of an object

// define some services
var self=this;
container.set('baz',function (c) {
    return {
        foo:c.foo,
        bar:c.bar
    };
});

Notice that the anonymous function has access to the current container instance, allowing references to other services or parameters.

As objects are only created when you get them, the order of the definitions does not matter, and there is no performance penalty.

Using the defined services is also very easy

// get the session object in browsers that support property descriptors
var baz = container.baz;
//or in old browsers
baz = container.get('baz');

####Protecting Parameters

Because Pimple sees anonymous functions as service definitions, you need to wrap anonymous functions with the protect() method to store them as parameter

container.set('random',container.protect(function () { return Math.random(); }));

var randomNumber = container.random();

####Modifying services after creation

In some cases you may want to modify a service definition after it has been defined. You can use the extend() method to define additional code to be run on your service just after it is created

container.set('mail',function (c) {
    return new \Mail();
});

container.extend('mail', function(mail, c) {
    mail.setFrom(c.mail.default_from);
    return mail;
});

The first argument is the name of the object, the second is a function that gets access to the object instance and the container.

Fetching the service creation function

When you access an object, Pimple automatically calls the anonymous function that you defined, which creates the service object for you. If you want to get raw access to this function, you can use the raw() method

container.set('session', function (c) {
    return new Session(c.session_storage);
});

var sessionFunction = container.raw('session');

####Extending a Container

If you use the same libraries over and over, you might want to reuse some services from one project to the other; package your services into a provider;

var provider = {
    register:function(container){
        // register some services and parameters
        // on pimple
    }
}

Then, the provider can be easily registered on a Container:

container.register(provider);

####Defining Factory Services

By default, each time you get a service, Pimple returns the same instance of it. If you want a different instance to be returned for all calls, wrap your anonymous function with the factory() method

container.factory('session',function (c) {
        return new Session(c.session_storage);
});