-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.coffee
281 lines (209 loc) · 6.33 KB
/
controller.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'use strict'
define (require) ->
_ = require 'underscore'
Backbone = require 'backbone'
Backbone.Marionette = require 'backbone.marionette'
Phos = {}
Helpers = require "./helpers/_helpers_"
###
Abstraction of the Marionette Controller
###
class Phos.Controller extends Backbone.Marionette.Controller
###
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 context of the application.
@property {Phos.Context}
###
appContext: null
###
The context of the Module instantiating this Controller
@property {Phos.Context}
###
modContext: null
###
Regions controlled by this Controller.
The region property contains a string which is simply the DOM id for the
container element of the region.
eg.
regions:
moduleRegion: '#region-module'
@property {Object<String>}
###
regions: null
###
Views of this Controller
eg.
views:
layout: LayoutView
form: FormView
@property {Object}
###
views: null
###
Data for each View on the Controller.
The viewData object should directly correspond to the same properties
specified on the views object. The string provided should match a setting
that was specified in the Module and is accessible through the modContext.
eg.
viewData:
layout: 'someSetting'
form: 'anotherSetting'
@property {Object}
###
viewData: null
###
The Controller Router
@property {Object}
###
router: null
###
The routes and corresponding methods this Controller responds to.
By setting a route method to "restful", it will automatically generate the
routes and corresponding method for a restful interface based on that route.
You must provide the following methods to the controller;
index, create, show, edit
eg.
routes:
properties: 'restful'
--->
/properties => index
/properties/new => create
/properties/:id => show
/properties/:id/edit => edit
@property {Object}
###
routes: null
###
Constructor
@param options {Object}
@option options {Phos.Content} context
###
constructor: (options) ->
@appContext = options.appContext
@modContext = options.modContext
@app = @appContext.getApp()
# Set module regions
@app.addRegions(@regions) if @regions?
# Initialize Controller Routing
@router = new Helpers.Router(
controller: @
routes: @routes
)
# Initialize the Views
for viewName, View of @views
if @viewData?
viewData = @viewData[viewName]
viewData = @modContext.get(viewData) if typeof viewData is 'string'
@views[viewName] = new View(
appContext: @appContext
modContext: @modContext
viewData: viewData
) if 'function' is typeof View
# Initialize the Controller
super
# Start Controller mediators
@startMediating()
# Start View mediators
@startViewMediators()
return @
###
Close the Regions on the Controller
@method onClose
###
onClose: ->
for Region of @regions
for index, View of @views
if View is @app[Region].currentView
try
@app[Region].close()
catch e
logger.warn e
else if not View.isClosed
View.close()?
return
###
Start controller mediators. Any mediator event publishing and subscribing
between the view to the controller and vice versa should happen here.
@method startMediating
@placeholder
###
startMediating: ->
###
Start the View mediators
@method startViewMediators
###
startViewMediators: ->
@subscribe 'controller:view:closing', =>
@stopViewMediators()
for index, view of @views
view.startMediating() if view.startMediating?
@
###
Remove the View mediators
@method stopViewMediators
###
stopViewMediators: ->
_.each @_events, (callbacks, event) =>
view = _.first(event.split(':'))
delete @_events[event] if @views[view]?
###
Resets a the data on a View. Must be executed prior to the View rendering.
This implementation will likely change to accomodate more complex data
queries.
@method resetViewData
@param options {Object}
@option options {String} method The Controller method to run
@option options {Boolean} close Pass false to prevent closing the view first.
@option options {String} view The View to close and reset the View Data on
@option options {String} setting The property for the View Data in the Module Context
@option options {Object} data The Model or Collection for the View
###
resetViewData: (options) ->
# Close the view, removing mediators, knockout bindings, and jquery bindings
@views[options.view].close()
# Add the new Model or Collection back into the Module Context
@modContext.set(_.object(["#{options.setting}"], [options.data])) if options.data?
# Set the viewData object for the View
@views[options.view].viewData = @modContext.get(options.setting)
# Reset the ViewModel for the View
@views[options.view].setViewModel()
# Run the Controller method
@[options.method]() if options.method?
###
Publish event
@method publish
@param eventName {String}
###
publish: (eventName) ->
@trigger.apply @, arguments
###
Subscribe
@method subscribe
@param eventName {String}
@param callback {Function}
###
subscribe: (eventName, callback) ->
args = _.toArray(arguments)
args.unshift @
@listenTo.apply @, args
###
Unsubscribe
@method unsubscribe
@param eventName {String}
@param callback {Function} Optional
###
unsubscribe: (eventName, callback) ->
args = _.toArray(arguments)
args.unshift controller
@stopListening.apply @, args