This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
forked from justspamjustin/BossView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Marionette.BossView.AMD.js
221 lines (190 loc) · 7.47 KB
/
Marionette.BossView.AMD.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
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
/**
* https://github.com/justspamjustin/BossView
* BossView v 0.1.4
*/
define(function (require) {
'use strict';
var _ = require('underscore');
var Marionette = require('marionette');
Marionette.BossView = Marionette.ItemView.extend({
template: function () { return ''; },
constructor: function () {
Marionette.ItemView.prototype.constructor.apply(this, arguments);
this._initializeSubViews();
this._afterInitializeSubViews();
this.listenTo(this, 'render', this._onParentRendered);
},
getParentEl: function () {
return this.$el;
},
_afterInitializeSubViews: function() {
this._initializeChildViewEvents();
this._initializeSubViewEventBubbling();
},
_initializeSubViews: function () {
this.initializedSubViews = {};
this._eachSubView(_.bind(this._initializeSubView, this));
},
_initializeSubView: function(subViewName, subViewFunction) {
var subView = this._getInitializedSubView(subViewFunction);
this._checkSubViewForRender(subView, subViewName);
this[subViewName] = subView;
this.initializedSubViews[subViewName] = subView;
},
initializeSubView: function(subViewName, subViewFunction) {
this._initializeSubView(subViewName, subViewFunction);
this._afterInitializeSubViews();
},
_getInitializedSubView: function (subViewFunction) {
var subView;
var isRenderableView = _.isFunction(subViewFunction.prototype.render);
if (isRenderableView) {
subView = this._initializeRenderableSubView(subViewFunction);
} else {
subView = subViewFunction.call(this);
}
return subView;
},
_initializeRenderableSubView: function (subViewFunction) {
return new subViewFunction({
model: this.model,
collection: this.collection
});
},
_checkSubViewForRender: function (subView, subViewName) {
if (_.isUndefined(subView) || !_.isFunction(subView.render)) {
throw new Error('The subview named ' + subViewName + ' does not have a render function.');
}
},
_initializeChildViewEvents: function () {
this._eachSubViewEvent(_.bind(function (subView, subViewEventName, subViewEventCallback) {
subViewEventCallback = this._getSubViewEventCallbackFunction(subViewEventCallback, subViewEventName);
if (subView === '*') {
this._listenToEventOnAllSubViews(subViewEventCallback, subViewEventName);
} else {
if (subView) {
this.listenTo(subView, subViewEventName, subViewEventCallback);
}
}
}, this));
},
_getSubViewEventCallbackFunction: function (subViewEventCallback, subViewEventName) {
if (_.isString(subViewEventCallback)) {
this._checkForSubViewEventCallback(subViewEventCallback, subViewEventName);
subViewEventCallback = this[subViewEventCallback];
}
return subViewEventCallback;
},
_listenToEventOnAllSubViews: function (subViewEventCallback, subViewEventName) {
this._eachSubView(_.bind(function (subViewName) {
var subViewInstance = this[subViewName];
this.listenTo(subViewInstance, subViewEventName, subViewEventCallback);
}, this));
},
_checkForSubViewEventCallback: function (subViewEventCallback, subViewEventName) {
if (_.isUndefined(this[subViewEventCallback])) {
throw new Error('This view has no function named ' + subViewEventCallback + ' to use as a callback for the event ' + subViewEventName);
}
},
_initializeSubViewEventBubbling: function () {
this._eachSubView(_.bind(function (subViewName) {
var subView = this[subViewName];
this.listenTo(subView, 'all', function () {
this.trigger(subViewName + ':' + arguments[0], arguments[1]);
});
}, this));
},
_onParentRendered: function () {
this.trigger('subviews:before:render');
this._renderSubViews();
this.trigger('subviews:after:render');
},
_renderSubViews: function () {
this._eachSubView(_.bind(this.renderSubView, this));
},
renderSubView: function(subViewName) {
var mainSubViewContainer = this._getOption('mainSubViewContainer');
var appendToEl = this.getParentEl();
if (this._hasSubViewContainer(subViewName)) {
appendToEl = this._getSubViewContainer(subViewName);
} else if (mainSubViewContainer) {
appendToEl = this.$(mainSubViewContainer);
}
this._renderSubView(subViewName, appendToEl);
},
_renderSubView: function (subViewName, appendToEl) {
if (this._shouldRenderSubView(subViewName)) {
this[subViewName].render().$el.appendTo(appendToEl);
/**
* We need to call delegateEvents here because when Marionette renders a template
* it uses this.$el.html(templateHTML). If this is the second render, then it will
* remove each of the subViews from the DOM, thus also unbinding each of their DOM
* events. So this is necessary for any renders after the initial render.
*/
this[subViewName].delegateEvents();
}
},
_shouldRenderSubView: function (subViewName) {
var renderConditionFunction = this._getSubViewRenderConditions()[subViewName];
var hasRenderConditionFunction = _.isFunction(renderConditionFunction);
return hasRenderConditionFunction ? renderConditionFunction.call(this) : true;
},
_eachSubView: function (callback) {
if (this._getSubViews()) {
for (var subViewName in this._getSubViews()) {
callback(subViewName, this._getSubViews()[subViewName]);
}
}
},
_eachSubViewEvent: function (callback) {
var subViewEvents = this._getOption('subViewEvents');
if (subViewEvents) {
for (var subViewEventKey in subViewEvents) {
var split = this._splitSubViewEventKey(subViewEventKey);
var subView = split.subViewName === '*' ? '*' : this[split.subViewName];
callback(subView, split.subViewEventName, subViewEvents[subViewEventKey]);
}
}
},
_splitSubViewEventKey: function (subViewEventKey) {
var subViewEventKeySplit = subViewEventKey.split(' ');
return {
subViewName: subViewEventKeySplit[0],
subViewEventName: subViewEventKeySplit[1]
}
},
_hasSubViewContainer: function (subViewName) {
var subViewContainers = this._getOption('subViewContainers');
return !_.isUndefined(subViewContainers) && !_.isUndefined(subViewContainers[subViewName]);
},
_getSubViewContainer: function (subViewName) {
if (!this._hasSubViewContainer(subViewName)) {
throw new Error('No subview container for subView: ' + subViewName);
}
return this.$(this._getOption('subViewContainers')[subViewName]);
},
remove: function () {
Marionette.ItemView.prototype.remove.apply(this, arguments);
this._removeSubViews();
},
_removeSubViews: function () {
_.each(this.initializedSubViews, function(subView) {
subView.remove();
});
},
_getSubViews: function () {
var subViews = _.result(this, 'subViews');
if (this.options.subViews) {
subViews = _.result(this.options, 'subViews');
}
return subViews;
},
_getOption: function (optionName) {
return this[optionName] || this.options[optionName];
},
_getSubViewRenderConditions: function () {
return this._getOption('subViewRenderConditions') || {};
}
});
return Marionette.BossView;
});