-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
88 lines (61 loc) · 1.98 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
var nd = require('./nodependency');
var fooFactory = function () {
console.log('*******\nFactory method called\n*******');
return {
bar: function (baz) {
return baz;
}
};
}
var transientFactory = function () {
return new Date().toString();
}
nd.lifeCycle('transient')
.factory('transientValue', transientFactory);
console.log('Transient factory: ', nd.factory('transientValue'));
setTimeout(function () {
console.log('Transient factory: ', nd.factory('transientValue'));
}, 1500);
var singletonFactory = function () {
return new Date().toString();
}
nd.lifeCycle('singleton')
.factory('singletonValue', singletonFactory);
console.log('Singleton factory: ', nd.factory('singletonValue'));
setTimeout(function () {
console.log('Singleton factory: ', nd.factory('singletonValue'));
}, 1500);
var bazInstance = {
doSomething: function () {
return 'Did something!';
}
}
nd.instance('baz', bazInstance);
nd.lifeCycle('firstLifeCycle')
.factory('foo', fooFactory);
console.log('Getting value from factory');
var foo = nd.factory('foo');
// {
// foo : function () {
// return 'foo';
// }
// }
var firstController = function ( foo, baz ) {
console.log('Called controller function with parameters: ', foo, ', ', baz);
return foo.bar(baz.doSomething());
}
//this returns the function you passed it, bound to its dependency-injected arguments
var injectedController = nd.inject(firstController);
//now call your method without parameters
var controllerReturn = injectedController();
console.log('controller return: ', controllerReturn);
// 'bar'
console.log('Getting cached value for factory')
// will be cached
var newFoo = nd.factory('foo');
console.log(newFoo.bar('Got cached foo'));
console.log('Expiring lifecycle');
nd.lifeCycle('firstLifeCycle').expire();
console.log('Getting factory result after expiry');
var uncachedFoo = nd.factory('foo');
console.log('Result after lifeCycle expiry: ', uncachedFoo.bar('Dependency injection is the coolest!'));