Skip to content

Commit

Permalink
Merge pull request #81 from young-steveo/release-1.6.0
Browse files Browse the repository at this point in the history
Release 1.6.0
  • Loading branch information
young-steveo authored Feb 22, 2017
2 parents fd370a5 + 96ddde1 commit 6da756e
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 34 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ $ npm install bottlejs
BottleJS is also available on cdnjs:

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/bottlejs/1.4.0/bottle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bottlejs/VERSION/bottle.min.js"></script>
```
Replace `VERSION` in the above URL with a valid BottleJS version, e.g. `https://cdnjs.cloudflare.com/ajax/libs/bottlejs/1.5.0/bottle.min.js`

## Simple Example

Expand Down Expand Up @@ -304,8 +305,9 @@ Param | Type | Details
**value** | *Mixed* | A value that will be defined as enumerable, but not writable.

#### decorator(name, func)
#### container.$decorator(name, func)

Used to register a decorator function that the provider will use to modify your services at creation time.
Used to register a decorator function that the provider will use to modify your services at creation time. `bottle.container.$decorator` is an alias of `bottle.decorator`; this allows you to only add a decorator to a nested bottle.

Param | Type | Details
:--------------------------|:-----------|:--------
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bottlejs",
"version": "1.5.0",
"version": "1.6.0",
"description": "A powerful dependency injection micro container",
"main": "dist/bottle.min.js",
"license": "MIT",
Expand Down
7 changes: 7 additions & 0 deletions dist/bottle.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare class Bottle {
static pop: (name?: string) => Bottle;
static clear: (name?: string) => void;
static list: (container?: Bottle.IContainer) => Array<string>;
static config: Object;

public container: Bottle.IContainer;
Expand Down Expand Up @@ -58,6 +59,11 @@ declare class Bottle {
*/
provider(name: string, Provider: ((...any: any[]) => void)): this;

/**
* Reset providers on the bottle instance.
*/
resetProviders(): void;

/**
* Register a service, factory, provider, or value based on properties of the Obj.
*/
Expand Down Expand Up @@ -91,6 +97,7 @@ declare module Bottle {
}

interface IContainer {
$decorator(name: string|((service: any) => any), func?: (service: any) => any): this;
$register(Obj: Bottle.IRegisterableObject): this;
$list(container?: Bottle.IContainer): Array<string>;
}
Expand Down
45 changes: 39 additions & 6 deletions dist/bottle.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
;(function(undefined) {
'use strict';
/**
* BottleJS v1.5.0 - 2016-10-14
* BottleJS v1.6.0 - 2017-02-22
* A powerful dependency injection micro container
*
* Copyright (c) 2016 Stephen Young
* Copyright (c) 2017 Stephen Young
* Licensed MIT
*/

Expand Down Expand Up @@ -166,7 +166,7 @@
* A filter function for removing bottle container methods and providers from a list of keys
*/
var byMethod = function byMethod(name) {
return !/^\$(?:register|list)$|Provider$/.test(name);
return !/^\$(?:decorator|register|list)$|Provider$/.test(name);
};

/**
Expand Down Expand Up @@ -262,7 +262,7 @@
*/
var pop = function pop(name) {
var instance;
if (name) {
if (typeof name === 'string') {
instance = bottles[name];
if (!instance) {
bottles[name] = instance = new Bottle();
Expand All @@ -277,7 +277,7 @@
* Clear all named bottles.
*/
var clear = function clear(name) {
if (name) {
if (typeof name === 'string') {
delete bottles[name];
} else {
bottles = {};
Expand Down Expand Up @@ -308,6 +308,7 @@
if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + 'Provider']) {
return console.error(fullname + ' provider already instantiated.');
}
this.originalProviders[fullname] = Provider;
this.providerMap[fullname] = true;

name = parts.shift();
Expand All @@ -331,7 +332,6 @@
/**
* Create the provider properties on the container
*
* @param String fullname
* @param String name
* @param Function Provider
* @return Bottle
Expand Down Expand Up @@ -417,6 +417,36 @@
return this[Obj.$type || 'service'].apply(this, [Obj.$name, value].concat(Obj.$inject || []));
};

/**
* Deletes providers from the map and container.
*
* @param String name
* @return void
*/
var removeProviderMap = function resetProvider(name) {
delete this.providerMap[name];
delete this.container[name];
delete this.container[name + 'Provider'];
};

/**
* Resets all providers on a bottle instance.
*
* @return void
*/
var resetProviders = function resetProviders() {
var providers = this.originalProviders;
Object.keys(this.originalProviders).forEach(function resetPrvider(provider) {
var parts = provider.split('.');
if (parts.length > 1) {
removeProviderMap.call(this, parts[0]);
parts.forEach(removeProviderMap, getNestedBottle.call(this, parts[0]));
}
removeProviderMap.call(this, provider);
this.provider(provider, providers[provider]);
}, this);
};


/**
* Execute any deferred functions
Expand Down Expand Up @@ -516,8 +546,10 @@
this.middlewares = {};
this.nested = {};
this.providerMap = {};
this.originalProviders = {};
this.deferred = [];
this.container = {
$decorator : decorator.bind(this),
$register : register.bind(this),
$list : list.bind(this)
};
Expand All @@ -536,6 +568,7 @@
list : list,
middleware : middleware,
provider : provider,
resetProviders : resetProviders,
register : register,
resolve : resolve,
service : service,
Expand Down
6 changes: 3 additions & 3 deletions dist/bottle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6da756e

Please sign in to comment.