-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.coffee
191 lines (142 loc) · 4.5 KB
/
module.coffee
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'use strict'
define (require) ->
_ = require 'underscore'
Phos = {}
Phos.Context = require './context'
Helpers = require "./helpers/_helpers_"
###
Abstraction of the Marionette Module with module event helpers
###
class Phos.Module
###
Console logging
@private
@property {Phos.Helpers.Logger}
###
logger = new Helpers.Logger()
###
The Marionette application instance provided by the application context
@private
@property {Object}
###
app = null
###
The name of the Module used by the application instance
@property {String}
###
moduleName: null
###
Enable or disable the auto start of the Module. Defaults to true.
@property {Boolean}
###
startWithParent: true
###
The global context of the application
@property {Phos.Context}
###
appContext: null
###
The context of the module
@property {Phos.Context}
###
modContext: null
###
Module Dock items. Each module defines its own "dock item(s)" aka navigation
items for the module.
Any type of property structure may be used for the dock item. The property
structure should remain consistent and a Collection with a Model should be
used to store the items and define the default properties of the dock item.
eg. [
{
label: 'Module Name'
priority: 1000
color: 'blue'
href: '/route'
}
]
@property {Array}
###
moduleDock: null
###
Sub Modules of this Module
@property {Array<Phos.Module>}
###
modules: null
###
The Controller functioning on this Module
@property {Object}
###
Controller: null
###
Constructor
@param options {Object}
@option options {Phos.Context} context The context of the module
@option options {Boolean} autoStart Enable or disable auto starting the Module
###
constructor: (options) ->
if not options.appContext?
return logger.error @constructor.name, 'Please provide the application context.'
else
@appContext = options.appContext
@modContext = new Phos.Context(options.modContext if options.modContext?)
app = @appContext.getApp() unless app?
# Set the Module Name
unless @moduleName?
return logger.error @constructor.name, 'Please provide a Module Name.'
# Create the Module
self = @
app.module(@moduleName, -> _.extend(@, self))
###
The before start method is an event that is triggered right before the Module
is started. Initialize the Module Controller here
In addition you can add initializers through the Module addInitializer
method.
@method onBeforeStart
###
onBeforeStart: ->
if typeof @Controller is 'function'
@Controller = new @Controller(
appContext: @appContext
modContext: @modContext
)
else if @Controller? and @Controller.initialize?
@Controller.initialize()
###
The on start method is an event that is triggered after the Module has
started. We start sub modules here so we are sure the parent module has
initialized and setup any views or data that may be required by a sub
module.
@method onStart
###
onStart: ->
# Create the sub modules
_.each @modules, (Module, i) =>
new Module(
appContext: @appContext
modContext: @modContext
) if typeof Module is 'function'
# Send out an event to add the Dock items for the Module
@appContext.publish('Core:Module:Dock:Add', @moduleDock) if @moduleDock?
###
The before stop method is an event that is triggered right before the Module
is stopped. Peform any pre-teardown functionality here that is not already
handled by Marionette. For example, a confirmation dialog if the user really
wants to close the module.
@placeholder
@method onBeforeStop
###
onBeforeStop: ->
###
The after stop method is an event that is triggered after the Module is
stopped. Peform any additional teardown functionality that needs to take place
here.
In addition you can add finalizers through the Module addFinalizer
method.
@placeholder
@method onAfterStop
###
onStop: ->
# Send out an event to remove the Dock items for the Module
@appContext.publish('Core:Module:Dock:Remove', @moduleDock) if @moduleDock?
# Close the controller
@Controller.close?()