-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
container_view.js
394 lines (311 loc) · 10.9 KB
/
container_view.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
require('ember-views/views/view');
require('ember-views/views/states');
require('ember-runtime/mixins/mutable_array');
var states = Ember.View.cloneStates(Ember.View.states);
/**
@module ember
@submodule ember-views
*/
var get = Ember.get, set = Ember.set;
var forEach = Ember.EnumerableUtils.forEach;
/**
A `ContainerView` is an `Ember.View` subclass that implements `Ember.MutableArray`
allowing programatic management of its child views.
## Setting Initial Child Views
The initial array of child views can be set in one of two ways. You can
provide a `childViews` property at creation time that contains instance of
`Ember.View`:
```javascript
aContainer = Ember.ContainerView.create({
childViews: [Ember.View.create(), Ember.View.create()]
});
```
You can also provide a list of property names whose values are instances of
`Ember.View`:
```javascript
aContainer = Ember.ContainerView.create({
childViews: ['aView', 'bView', 'cView'],
aView: Ember.View.create(),
bView: Ember.View.create(),
cView: Ember.View.create()
});
```
The two strategies can be combined:
```javascript
aContainer = Ember.ContainerView.create({
childViews: ['aView', Ember.View.create()],
aView: Ember.View.create()
});
```
Each child view's rendering will be inserted into the container's rendered
HTML in the same order as its position in the `childViews` property.
## Adding and Removing Child Views
The container view implements `Ember.MutableArray` allowing programatic management of its child views.
To remove a view, pass that view into a `removeObject` call on the container view.
Given an empty `<body>` the following code
```javascript
aContainer = Ember.ContainerView.create({
classNames: ['the-container'],
childViews: ['aView', 'bView'],
aView: Ember.View.create({
template: Ember.Handlebars.compile("A")
}),
bView: Ember.View.create({
template: Ember.Handlebars.compile("B")
})
});
aContainer.appendTo('body');
```
Results in the HTML
```html
<div class="ember-view the-container">
<div class="ember-view">A</div>
<div class="ember-view">B</div>
</div>
```
Removing a view
```javascript
aContainer.toArray(); // [aContainer.aView, aContainer.bView]
aContainer.removeObject(aContainer.get('bView'));
aContainer.toArray(); // [aContainer.aView]
```
Will result in the following HTML
```html
<div class="ember-view the-container">
<div class="ember-view">A</div>
</div>
```
Similarly, adding a child view is accomplished by adding `Ember.View` instances to the
container view.
Given an empty `<body>` the following code
```javascript
aContainer = Ember.ContainerView.create({
classNames: ['the-container'],
childViews: ['aView', 'bView'],
aView: Ember.View.create({
template: Ember.Handlebars.compile("A")
}),
bView: Ember.View.create({
template: Ember.Handlebars.compile("B")
})
});
aContainer.appendTo('body');
```
Results in the HTML
```html
<div class="ember-view the-container">
<div class="ember-view">A</div>
<div class="ember-view">B</div>
</div>
```
Adding a view
```javascript
AnotherViewClass = Ember.View.extend({
template: Ember.Handlebars.compile("Another view")
});
aContainer.toArray(); // [aContainer.aView, aContainer.bView]
aContainer.pushObject(AnotherViewClass.create());
aContainer.toArray(); // [aContainer.aView, aContainer.bView, <AnotherViewClass instance>]
```
Will result in the following HTML
```html
<div class="ember-view the-container">
<div class="ember-view">A</div>
<div class="ember-view">B</div>
<div class="ember-view">Another view</div>
</div>
```
## Templates and Layout
A `template`, `templateName`, `defaultTemplate`, `layout`, `layoutName` or
`defaultLayout` property on a container view will not result in the template
or layout being rendered. The HTML contents of a `Ember.ContainerView`'s DOM
representation will only be the rendered HTML of its child views.
## Binding a View to Display
If you would like to display a single view in your ContainerView, you can set
its `currentView` property. When the `currentView` property is set to a view
instance, it will be added to the ContainerView. If the `currentView` property
is later changed to a different view, the new view will replace the old view.
If `currentView` is set to `null`, the last `currentView` will be removed.
This functionality is useful for cases where you want to bind the display of
a ContainerView to a controller or state manager. For example, you can bind
the `currentView` of a container to a controller like this:
```javascript
App.appController = Ember.Object.create({
view: Ember.View.create({
templateName: 'person_template'
})
});
```
```handlebars
{{view Ember.ContainerView currentViewBinding="App.appController.view"}}
```
@class ContainerView
@namespace Ember
@extends Ember.View
*/
Ember.ContainerView = Ember.View.extend(Ember.MutableArray, {
states: states,
init: function() {
this._super();
var childViews = get(this, 'childViews');
// redefine view's childViews property that was obliterated
Ember.defineProperty(this, 'childViews', Ember.View.childViewsProperty);
var _childViews = this._childViews;
forEach(childViews, function(viewName, idx) {
var view;
if ('string' === typeof viewName) {
view = get(this, viewName);
view = this.createChildView(view);
set(this, viewName, view);
} else {
view = this.createChildView(viewName);
}
_childViews[idx] = view;
}, this);
var currentView = get(this, 'currentView');
if (currentView) {
_childViews.push(this.createChildView(currentView));
}
},
replace: function(idx, removedCount, addedViews) {
var addedCount = addedViews ? get(addedViews, 'length') : 0;
this.arrayContentWillChange(idx, removedCount, addedCount);
this.childViewsWillChange(this._childViews, idx, removedCount);
if (addedCount === 0) {
this._childViews.splice(idx, removedCount) ;
} else {
var args = [idx, removedCount].concat(addedViews);
this._childViews.splice.apply(this._childViews, args);
}
this.arrayContentDidChange(idx, removedCount, addedCount);
this.childViewsDidChange(this._childViews, idx, removedCount, addedCount);
return this;
},
objectAt: function(idx) {
return this._childViews[idx];
},
length: Ember.computed(function () {
return this._childViews.length;
}),
/**
@private
Instructs each child view to render to the passed render buffer.
@method render
@param {Ember.RenderBuffer} buffer the buffer to render to
*/
render: function(buffer) {
this.forEachChildView(function(view) {
view.renderToBuffer(buffer);
});
},
instrumentName: 'render.container',
/**
@private
When a child view is removed, destroy its element so that
it is removed from the DOM.
The array observer that triggers this action is set up in the
`renderToBuffer` method.
@method childViewsWillChange
@param {Ember.Array} views the child views array before mutation
@param {Number} start the start position of the mutation
@param {Number} removed the number of child views removed
**/
childViewsWillChange: function(views, start, removed) {
this.propertyWillChange('childViews');
if (removed > 0) {
var changedViews = views.slice(start, start+removed);
// transition to preRender before clearing parentView
this.currentState.childViewsWillChange(this, views, start, removed);
this.initializeViews(changedViews, null, null);
}
},
removeChild: function(child) {
this.removeObject(child);
return this;
},
/**
@private
When a child view is added, make sure the DOM gets updated appropriately.
If the view has already rendered an element, we tell the child view to
create an element and insert it into the DOM. If the enclosing container
view has already written to a buffer, but not yet converted that buffer
into an element, we insert the string representation of the child into the
appropriate place in the buffer.
@method childViewsDidChange
@param {Ember.Array} views the array of child views afte the mutation has occurred
@param {Number} start the start position of the mutation
@param {Number} removed the number of child views removed
@param {Number} the number of child views added
*/
childViewsDidChange: function(views, start, removed, added) {
if (added > 0) {
var changedViews = views.slice(start, start+added);
this.initializeViews(changedViews, this, get(this, 'templateData'));
this.currentState.childViewsDidChange(this, views, start, added);
}
this.propertyDidChange('childViews');
},
initializeViews: function(views, parentView, templateData) {
forEach(views, function(view) {
set(view, '_parentView', parentView);
if (!get(view, 'templateData')) {
set(view, 'templateData', templateData);
}
});
},
currentView: null,
_currentViewWillChange: Ember.beforeObserver(function() {
var currentView = get(this, 'currentView');
if (currentView) {
currentView.destroy();
}
}, 'currentView'),
_currentViewDidChange: Ember.observer(function() {
var currentView = get(this, 'currentView');
if (currentView) {
this.pushObject(currentView);
}
}, 'currentView'),
_ensureChildrenAreInDOM: function () {
this.currentState.ensureChildrenAreInDOM(this);
}
});
Ember.merge(states._default, {
childViewsWillChange: Ember.K,
childViewsDidChange: Ember.K,
ensureChildrenAreInDOM: Ember.K
});
Ember.merge(states.inBuffer, {
childViewsDidChange: function(parentView, views, start, added) {
throw new Error('You cannot modify child views while in the inBuffer state');
}
});
Ember.merge(states.hasElement, {
childViewsWillChange: function(view, views, start, removed) {
for (var i=start; i<start+removed; i++) {
views[i].remove();
}
},
childViewsDidChange: function(view, views, start, added) {
Ember.run.scheduleOnce('render', view, '_ensureChildrenAreInDOM');
},
ensureChildrenAreInDOM: function(view) {
var childViews = view._childViews, i, len, childView, previous, buffer;
for (i = 0, len = childViews.length; i < len; i++) {
childView = childViews[i];
buffer = childView.renderToBufferIfNeeded();
if (buffer) {
childView.triggerRecursively('willInsertElement');
if (previous) {
previous.domManager.after(previous, buffer.string());
} else {
view.domManager.prepend(view, buffer.string());
}
childView.transitionTo('inDOM');
childView.propertyDidChange('element');
childView.triggerRecursively('didInsertElement');
}
previous = childView;
}
}
});