Comparing version 1.3.0 to 1.4.0
@@ -35,2 +35,12 @@ declare class Bottle { | ||
/** | ||
* Register a service instance factory | ||
*/ | ||
instanceFactory(name: string, Factory: (container: Bottle.IContainer) => any): this; | ||
/** | ||
* List the services registered on the container | ||
*/ | ||
list(container?: Bottle.IContainer): Array<string>; | ||
/** | ||
* Register a middleware function. This function will be executed every time the service is accessed. | ||
@@ -44,3 +54,3 @@ */ | ||
*/ | ||
provider(name: string, Provider: ((...any) => void)): this; | ||
provider(name: string, Provider: ((...any: any[]) => void)): this; | ||
@@ -60,3 +70,3 @@ /** | ||
*/ | ||
service(name: string, Constructor: ((...any) => any), ...dependency: string[]): this; | ||
service(name: string, Constructor: ((...any: any[]) => any), ...dependency: string[]): this; | ||
@@ -83,2 +93,2 @@ /** | ||
} | ||
} | ||
} |
;(function(undefined) { | ||
'use strict'; | ||
/** | ||
* BottleJS v1.3.0 - 2016-04-29 | ||
* BottleJS v1.4.0 - 2016-08-03 | ||
* A powerful dependency injection micro container | ||
@@ -217,2 +217,18 @@ * | ||
/** | ||
* Register an instance factory inside a generic factory. | ||
* | ||
* @param {String} name - The name of the service | ||
* @param {Function} Factory - The factory function, matches the signature required for the | ||
* `factory` method | ||
* @return Bottle | ||
*/ | ||
var instanceFactory = function instanceFactory(name, Factory) { | ||
return factory.call(this, name, function GenericInstanceFactory(container) { | ||
return { | ||
instance : Factory.bind(Factory, container) | ||
}; | ||
}); | ||
}; | ||
/** | ||
* A filter function for removing bottle container methods and providers from a list of keys | ||
@@ -573,2 +589,3 @@ */ | ||
factory : factory, | ||
instanceFactory: instanceFactory, | ||
list : list, | ||
@@ -575,0 +592,0 @@ middleware : middleware, |
{ | ||
"name": "bottlejs", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A powerful dependency injection micro container", | ||
@@ -38,2 +38,2 @@ "main": "dist/bottle.js", | ||
} | ||
} | ||
} |
![BottleJS](/bottle-logo.jpg) | ||
# BottleJS [![Build Status](https://travis-ci.org/young-steveo/bottlejs.svg?branch=master)](https://travis-ci.org/young-steveo/bottlejs) | ||
# BottleJS | ||
[![Build Status](https://travis-ci.org/young-steveo/bottlejs.svg?branch=master)](https://travis-ci.org/young-steveo/bottlejs) | ||
[![npm version](https://badge.fury.io/js/bottlejs.svg)](https://badge.fury.io/js/bottlejs) | ||
[![dependencies Status](https://david-dm.org/young-steveo/bottlejs/status.svg)](https://david-dm.org/young-steveo/bottlejs) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/young-steveo/bottlejs/badge.svg)](https://snyk.io/test/github/young-steveo/bottlejs) | ||
[![NPM](https://nodei.co/npm/bottlejs.png?downloads=true&downloadRank=true)](https://nodei.co/npm/bottlejs/) | ||
[![NPM](https://nodei.co/npm-dl/bottlejs.png?months=9&height=3)](https://nodei.co/npm/bottlejs/) | ||
> A powerful dependency injection micro container | ||
@@ -279,2 +287,26 @@ | ||
#### instanceFactory(name, Factory) | ||
Used to register a service instance factory that will return an instance when called. | ||
Param | Type | Details | ||
:-----------|:-----------|:-------- | ||
**name** | *String* | The name of the service. Must be unique to each Bottle instance. | ||
**Factory** | *Function* | A function that should return a fully configured service object. This factory function will be executed when a new instance is created. Gets passed an instance of the container. | ||
```js | ||
var bottle = new Bottle(); | ||
var Hefeweizen = function(container) { return { abv: Math.random() * (6 - 4) + 4 }}; | ||
bottle.instanceFactory('Beer.Hefeweizen', Hefeweizen); | ||
var hefeFactory = bottle.container.Beer.Hefeweizen; // This is an instance factory with a single `instance` method | ||
var beer1 = hefeFactory.instance(); // Calls factory function to create a new instance | ||
var beer2 = hefeFactory.instance(); // Calls factory function to create a second new instance | ||
beer1 !== beer2 // true | ||
``` | ||
This pattern is especially useful for request based context objects that store state or things like database connections. See the documentation for Google Guice's [InjectingProviders](https://github.com/google/guice/wiki/InjectingProviders) for more examples. | ||
#### middleware(name, func) | ||
@@ -281,0 +313,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40314
680
370