-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.js
143 lines (127 loc) · 3.89 KB
/
application.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @license MIT http://troopjs.mit-license.org/
*/
define([
"./component",
"troopjs-core/component/signal/initialize",
"troopjs-core/component/signal/start",
"troopjs-core/component/signal/stop",
"troopjs-core/component/signal/finalize",
"troopjs-hub/emitter",
"when/when"
], function (Widget, initialize, start, stop, finalize, hub, when) {
"use strict";
/**
* The application widget serves as a container for all troop components that bootstrap the page.
* @class widget.application
* @extend widget.component
* @alias widget.application
*/
/**
* Triggered when the application starts
* @event hub/application/start
* @param {widget.application} application The started application
*/
/**
* Triggered when the application stops
* @event hub/application/stop
* @param {widget.application} application The stopped application
*/
/**
* Application components
* @private
* @readonly
* @property {core.component.emitter[]} components
*/
var ARRAY_SLICE = Array.prototype.slice;
var COMPONENTS = "components";
/**
* @method constructor
* @inheritdoc
* @param {jQuery|HTMLElement} $element The element that this widget should be attached to
* @param {String} displayName A friendly name for this widget
* @param {...core.component.emitter} component List of components to start before starting the application.
*/
return Widget.extend(function () {
this[COMPONENTS] = ARRAY_SLICE.call(arguments, 2);
}, {
"displayName": "widget/application",
/**
* @handler
* @localdoc Initialize all registered components (widgets and services) that are passed in from the {@link #method-constructor}.
* @inheritdoc
*/
"sig/initialize": function () {
var args = arguments;
return when.map(this[COMPONENTS], function (component) {
return initialize.apply(component, args);
});
},
/**
* @handler
* @inheritdoc #event-sig/start
* @localdoc weave this and all widgets that are within this element.
* @fires hub/application/start
*/
"sig/start": function () {
var me = this;
var args = arguments;
return when
.map(me[COMPONENTS], function (component) {
return start.apply(component, args);
})
.then(function () {
return me.weave.apply(me, args);
})
.then(function () {
return hub.emit("application/start", me);
});
},
/**
* @handler
* @inheritdoc #event-sig/stop
* @localdoc stop this and all woven widgets that are within this element.
* @fires hub/application/stop
*/
"sig/stop": function () {
var me = this;
var args = arguments;
return me
.unweave.apply(me, args).then(function () {
return when.map(me[COMPONENTS], function (child) {
return stop.apply(child, args);
});
})
.then(function () {
return hub.emit("application/stop", me);
});
},
/**
* @handler
* @inheritdoc
* @localdoc finalize all registered components (widgets and services) that are registered from the {@link #method-constructor}.
*/
"sig/finalize": function () {
var args = arguments;
return when.map(this[COMPONENTS], function (component) {
return finalize.apply(component, args);
});
},
/**
* Start the component life-cycle, sends out {@link #event-sig/initialize} and then {@link #event-sig/start}.
* @param {...*} [args] arguments
* @return {Promise}
* @fires sig/initialize
* @fires sig/start
*/
"start": start,
/**
* Stops the component life-cycle, sends out {@link #event-sig/stop} and then {@link #event-sig/finalize}.
* @param {...*} [args] arguments
* @return {Promise}
* @fires sig/stop
* @fires sig/finalize
*/
"stop": finalize
});
});