-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarionette.multiregion.js
284 lines (224 loc) · 10.2 KB
/
marionette.multiregion.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
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
282
283
284
(function(Backbone) {
var Marionette = Backbone.Marionette;
Marionette.MultiRegion = Marionette.Region.extend({
constructor: function() {
this.currentViews = [];
Marionette.Region.apply(this, arguments);
},
// Displays a backbone view instance inside of the region.
// Handles calling the `render` method for you. Reads content
// directly from the `el` attribute. Also calls an optional
// `onShow` and `onDestroy` method on your view, just after showing
// or just before destroying the view, respectively.
// The `preventDestroy` option can be used to prevent a view from
// the old view being destroyed on show.
// The `forceShow` option can be used to force a view to be
// re-rendered if it's already shown in the region.
show: function(view, options){
if (!this._ensureElement()) {
return;
}
this._ensureViewIsIntact(view);
var showOptions = options || {};
var isDifferentView = !this.hasView(view);
var preventDestroy = !!showOptions.preventDestroy;
var forceShow = !!showOptions.forceShow;
// We are only changing the view if there is a current view to change to begin with
var isChangingView = this.hasViews();
// Only destroy the current view if we don't want to `preventDestroy` and if
// the view given in the first argument is different than `currentView`
var _shouldDestroyView = isDifferentView && !preventDestroy;
// Only show the view given in the first argument if it is different than
// the current view or if we want to re-show the view. Note that if
// `_shouldDestroyView` is true, then `_shouldShowView` is also necessarily true.
var _shouldShowView = isDifferentView || forceShow;
if (isChangingView) {
this.triggerMethod('before:swapOut', this.currentViews, this, options);
}
this.currentViews.forEach(function(currentView) {
delete currentView._parent;
});
if (_shouldDestroyView) {
this.empty();
// A `destroy` event is attached to the clean up manually removed views.
// We need to detach this event when a new view is going to be shown as it
// is no longer relevant.
} else if (isChangingView && _shouldShowView) {
this.currentViews.forEach(function(currentView) {
currentView.off('destroy', currentView._parentRemove, this);
}, this);
}
if (_shouldShowView) {
// We need to listen for if a view is destroyed
// in a way other than through the region.
// If this happens we need to remove the reference
// to the currentView since once a view has been destroyed
// we can not reuse it.
view.once('destroy', (view._parentRemove = _.bind(this.remove, this, view)), this);
view.render();
view._parent = this;
if (isChangingView) {
this.triggerMethod('before:swap', view, this, options);
}
this.triggerMethod('before:show', view, this, options);
Marionette.triggerMethodOn(view, 'before:show', view, this, options);
// An array of views that we're about to display
var attachedRegion = Marionette.isNodeAttached(this.el);
// The views that we're about to attach to the document
// It's important that we prevent _getNestedViews from being executed unnecessarily
// as it's a potentially-slow method
var displayedViews = [];
var triggerBeforeAttach = showOptions.triggerBeforeAttach || this.triggerBeforeAttach;
var triggerAttach = showOptions.triggerAttach || this.triggerAttach;
if (attachedRegion && triggerBeforeAttach) {
displayedViews = this._displayedViews(view);
this._triggerAttach(displayedViews, 'before:');
}
// forward options to attachHtml so this one can
// know if the view needs to be attached before / after
this.attachHtml(view, options);
this.currentViews.push(view);
if (attachedRegion && triggerAttach) {
displayedViews = this._displayedViews(view);
this._triggerAttach(displayedViews);
}
if (isChangingView) {
this.triggerMethod('swap', view, this, options);
}
this.triggerMethod('show', view, this, options);
Marionette.triggerMethodOn(view, 'show', view, this, options);
return this;
}
return this;
},
// Add & displays a backbone view instance inside of the region.
// Handles calling the `render` method for you. Reads content
// directly from the `el` attribute. Also calls an optional
// `onShow` and `onDestroy` method on your view, just after showing
// or just before destroying the view, respectively.
// The `preventDestroy` option can be used to prevent a view from
// the old view being destroyed on show.
// The `forceShow` option can be used to force a view to be
// re-rendered if it's already shown in the region.
add: function(view, options){
if (!this._ensureElement()) {
return;
}
this._ensureViewIsIntact(view);
var showOptions = options || {};
var isDifferentView = !this.hasView(view);
var forceShow = !!showOptions.forceShow;
// Only show the view given in the first argument if it is different than
// the current view or if we want to re-show the view. Note that if
// `_shouldDestroyView` is true, then `_shouldShowView` is also necessarily true.
var _shouldShowView = isDifferentView || forceShow;
if (_shouldShowView) {
// We need to listen for if a view is destroyed
// in a way other than through the region.
// If this happens we need to remove the reference
// to the currentView since once a view has been destroyed
// we can not reuse it.
view.once('destroy', (view._parentRemove = _.bind(this.remove, this, view)), this);
view.render();
view._parent = this;
this.triggerMethod('before:show', view, this, options);
Marionette.triggerMethodOn(view, 'before:show', view, this, options);
// An array of views that we're about to display
var attachedRegion = Marionette.isNodeAttached(this.el);
// The views that we're about to attach to the document
// It's important that we prevent _getNestedViews from being executed unnecessarily
// as it's a potentially-slow method
var displayedViews = [];
var triggerBeforeAttach = showOptions.triggerBeforeAttach || this.triggerBeforeAttach;
var triggerAttach = showOptions.triggerAttach || this.triggerAttach;
if (attachedRegion && triggerBeforeAttach) {
displayedViews = this._displayedViews(view);
this._triggerAttach(displayedViews, 'before:');
}
// forward options to attachHtml so this one can
// know if the view needs to be attached before / after
this.attachHtml(view, options);
this.currentViews.push(view);
if (attachedRegion && triggerAttach) {
displayedViews = this._displayedViews(view);
this._triggerAttach(displayedViews);
}
this.triggerMethod('show', view, this, options);
Marionette.triggerMethodOn(view, 'show', view, this, options);
return this;
}
return this;
},
// Override this method to change how the new view is
// appended to the `$el` that the region is managing
// TODO: handle more options
attachHtml: function(view, options) {
// empty the node and append new view
// We can not use `.innerHTML` due to the fact that IE
// will not let us clear the html of tables and selects.
// We also do not want to use the more declarative `empty` method
// that jquery exposes since `.empty` loops over all of the children DOM
// nodes and unsets the listeners on each node. While this seems like
// a desirable thing, it comes at quite a high perf cost. For that reason
// we are simply clearing the html contents of the node.
// this.$el.html('');
this.el.appendChild(view.el);
},
// Destroy the current views, if there is one. If there is no
// current view, it does nothing and returns immediately.
empty: function() {
// If there is no view in the region
// we should not remove anything
if (!this.hasViews()) { return; }
// remove one by one
// TODO: maybe better perf if bulk removed
_.forEach(this.currentViews, this.remove, this);
return this;
},
// Destroy the view given in parameter. If this view is not
// shown, it does nothing and return immediately.
remove: function(view) {
// If there is no view in the region
// we should not remove anything
if (!this.hasView(view)) { return; }
view.off('destroy', view._parentRemove, this);
this.triggerMethod('before:empty', view);
this._destroyView(view);
this.triggerMethod('empty', view);
// Remove region pointer to the currentViews
this.currentViews = _.without(this.currentViews, view);
return this;
},
// call 'destroy' or 'remove', depending on which is found
// on the view (if showing a raw Backbone view or a Marionette View)
_destroyView: function(view) {
if (view.destroy && !view.isDestroyed) {
view.destroy();
} else if (view.remove) {
view.remove();
// appending isDestroyed to raw Backbone View allows regions
// to throw a ViewDestroyedError for this view
view.isDestroyed = true;
}
},
// Attach an existing view to the region. This
// will not call `render` or `onShow` for the new view,
// and will not replace the current HTML for the `el`
// of the region.
attachView: function(view) {
if (!this.hasView(view)) {
this.currentViews.push(view);
}
return this;
},
// Checks whether a view is currently present within
// the region. Returns `true` if there is and `false` if
// no view is present.
hasView: function(view) {
return _.contains(this.currentViews, view);
},
hasViews: function() {
return this.currentViews.length > 0;
}
});
})(Backbone);