Skip to content

Commit 6da756e

Browse files
authored
Merge pull request #81 from young-steveo/release-1.6.0
Release 1.6.0
2 parents fd370a5 + 96ddde1 commit 6da756e

9 files changed

Lines changed: 86 additions & 34 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ $ npm install bottlejs
4141
BottleJS is also available on cdnjs:
4242

4343
```html
44-
<script src="https://cdnjs.cloudflare.com/ajax/libs/bottlejs/1.4.0/bottle.min.js"></script>
44+
<script src="https://cdnjs.cloudflare.com/ajax/libs/bottlejs/VERSION/bottle.min.js"></script>
4545
```
46+
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`
4647

4748
## Simple Example
4849

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

306307
#### decorator(name, func)
308+
#### container.$decorator(name, func)
307309

308-
Used to register a decorator function that the provider will use to modify your services at creation time.
310+
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.
309311

310312
Param | Type | Details
311313
:--------------------------|:-----------|:--------

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bottlejs",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "A powerful dependency injection micro container",
55
"main": "dist/bottle.min.js",
66
"license": "MIT",

dist/bottle.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare class Bottle {
22
static pop: (name?: string) => Bottle;
33
static clear: (name?: string) => void;
4+
static list: (container?: Bottle.IContainer) => Array<string>;
45
static config: Object;
56

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

62+
/**
63+
* Reset providers on the bottle instance.
64+
*/
65+
resetProviders(): void;
66+
6167
/**
6268
* Register a service, factory, provider, or value based on properties of the Obj.
6369
*/
@@ -91,6 +97,7 @@ declare module Bottle {
9197
}
9298

9399
interface IContainer {
100+
$decorator(name: string|((service: any) => any), func?: (service: any) => any): this;
94101
$register(Obj: Bottle.IRegisterableObject): this;
95102
$list(container?: Bottle.IContainer): Array<string>;
96103
}

dist/bottle.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
;(function(undefined) {
22
'use strict';
33
/**
4-
* BottleJS v1.5.0 - 2016-10-14
4+
* BottleJS v1.6.0 - 2017-02-22
55
* A powerful dependency injection micro container
66
*
7-
* Copyright (c) 2016 Stephen Young
7+
* Copyright (c) 2017 Stephen Young
88
* Licensed MIT
99
*/
1010

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

172172
/**
@@ -262,7 +262,7 @@
262262
*/
263263
var pop = function pop(name) {
264264
var instance;
265-
if (name) {
265+
if (typeof name === 'string') {
266266
instance = bottles[name];
267267
if (!instance) {
268268
bottles[name] = instance = new Bottle();
@@ -277,7 +277,7 @@
277277
* Clear all named bottles.
278278
*/
279279
var clear = function clear(name) {
280-
if (name) {
280+
if (typeof name === 'string') {
281281
delete bottles[name];
282282
} else {
283283
bottles = {};
@@ -308,6 +308,7 @@
308308
if (this.providerMap[fullname] && parts.length === 1 && !this.container[fullname + 'Provider']) {
309309
return console.error(fullname + ' provider already instantiated.');
310310
}
311+
this.originalProviders[fullname] = Provider;
311312
this.providerMap[fullname] = true;
312313

313314
name = parts.shift();
@@ -331,7 +332,6 @@
331332
/**
332333
* Create the provider properties on the container
333334
*
334-
* @param String fullname
335335
* @param String name
336336
* @param Function Provider
337337
* @return Bottle
@@ -417,6 +417,36 @@
417417
return this[Obj.$type || 'service'].apply(this, [Obj.$name, value].concat(Obj.$inject || []));
418418
};
419419

420+
/**
421+
* Deletes providers from the map and container.
422+
*
423+
* @param String name
424+
* @return void
425+
*/
426+
var removeProviderMap = function resetProvider(name) {
427+
delete this.providerMap[name];
428+
delete this.container[name];
429+
delete this.container[name + 'Provider'];
430+
};
431+
432+
/**
433+
* Resets all providers on a bottle instance.
434+
*
435+
* @return void
436+
*/
437+
var resetProviders = function resetProviders() {
438+
var providers = this.originalProviders;
439+
Object.keys(this.originalProviders).forEach(function resetPrvider(provider) {
440+
var parts = provider.split('.');
441+
if (parts.length > 1) {
442+
removeProviderMap.call(this, parts[0]);
443+
parts.forEach(removeProviderMap, getNestedBottle.call(this, parts[0]));
444+
}
445+
removeProviderMap.call(this, provider);
446+
this.provider(provider, providers[provider]);
447+
}, this);
448+
};
449+
420450

421451
/**
422452
* Execute any deferred functions
@@ -516,8 +546,10 @@
516546
this.middlewares = {};
517547
this.nested = {};
518548
this.providerMap = {};
549+
this.originalProviders = {};
519550
this.deferred = [];
520551
this.container = {
552+
$decorator : decorator.bind(this),
521553
$register : register.bind(this),
522554
$list : list.bind(this)
523555
};
@@ -536,6 +568,7 @@
536568
list : list,
537569
middleware : middleware,
538570
provider : provider,
571+
resetProviders : resetProviders,
539572
register : register,
540573
resolve : resolve,
541574
service : service,

dist/bottle.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)