forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.js
2209 lines (1741 loc) · 62.5 KB
/
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Ember.assert, Ember.deprecate, Ember.warn, Ember.TEMPLATES,
// jQuery, Ember.lookup,
// Ember.ContainerView circular dependency
// Ember.ENV
import Ember from 'ember-metal/core';
import create from 'ember-metal/platform/create';
import Evented from "ember-runtime/mixins/evented";
import EmberObject from "ember-runtime/system/object";
import EmberError from "ember-metal/error";
import { get } from "ember-metal/property_get";
import { set } from "ember-metal/property_set";
import setProperties from "ember-metal/set_properties";
import run from "ember-metal/run_loop";
import { addObserver, removeObserver } from "ember-metal/observer";
import { defineProperty } from "ember-metal/properties";
import {
guidFor,
typeOf
} from "ember-metal/utils";
import { computed } from "ember-metal/computed";
import {
Mixin,
observer,
beforeObserver
} from "ember-metal/mixin";
import KeyStream from "ember-views/streams/key_stream";
import StreamBinding from "ember-metal/streams/stream_binding";
import ContextStream from "ember-views/streams/context_stream";
import isNone from 'ember-metal/is_none';
import { deprecateProperty } from "ember-metal/deprecate_property";
import { A as emberA } from "ember-runtime/system/native_array";
import {
streamifyClassNameBinding
} from "ember-views/streams/class_name_binding";
// ES6TODO: functions on EnumerableUtils should get their own export
import {
forEach,
addObject,
removeObject
} from "ember-metal/enumerable_utils";
import {
propertyWillChange,
propertyDidChange
} from "ember-metal/property_events";
import jQuery from "ember-views/system/jquery";
import "ember-views/system/ext"; // for the side effect of extending Ember.run.queues
import CoreView from "ember-views/views/core_view";
import {
subscribe,
read,
isStream
} from "ember-metal/streams/utils";
import sanitizeAttributeValue from "ember-views/system/sanitize_attribute_value";
import { normalizeProperty } from "morph/dom-helper/prop";
function K() { return this; }
// Circular dep
var _renderView;
function renderView(view, buffer, template) {
if (_renderView === undefined) {
_renderView = require('ember-htmlbars/system/render-view')['default'];
}
_renderView(view, buffer, template);
}
/**
@module ember
@submodule ember-views
*/
var childViewsProperty = computed(function() {
var childViews = this._childViews;
var ret = emberA();
forEach(childViews, function(view) {
var currentChildViews;
if (view.isVirtual) {
if (currentChildViews = get(view, 'childViews')) {
ret.pushObjects(currentChildViews);
}
} else {
ret.push(view);
}
});
ret.replace = function (idx, removedCount, addedViews) {
throw new EmberError("childViews is immutable");
};
return ret;
});
Ember.warn("The VIEW_PRESERVES_CONTEXT flag has been removed and the functionality can no longer be disabled.", Ember.ENV.VIEW_PRESERVES_CONTEXT !== false);
/**
Global hash of shared templates. This will automatically be populated
by the build tools so that you can store your Handlebars templates in
separate files that get loaded into JavaScript at buildtime.
@property TEMPLATES
@for Ember
@type Hash
*/
Ember.TEMPLATES = {};
var EMPTY_ARRAY = [];
/**
`Ember.View` is the class in Ember responsible for encapsulating templates of
HTML content, combining templates with data to render as sections of a page's
DOM, and registering and responding to user-initiated events.
## HTML Tag
The default HTML tag name used for a view's DOM representation is `div`. This
can be customized by setting the `tagName` property. The following view
class:
```javascript
ParagraphView = Ember.View.extend({
tagName: 'em'
});
```
Would result in instances with the following HTML:
```html
<em id="ember1" class="ember-view"></em>
```
## HTML `class` Attribute
The HTML `class` attribute of a view's tag can be set by providing a
`classNames` property that is set to an array of strings:
```javascript
MyView = Ember.View.extend({
classNames: ['my-class', 'my-other-class']
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view my-class my-other-class"></div>
```
`class` attribute values can also be set by providing a `classNameBindings`
property set to an array of properties names for the view. The return value
of these properties will be added as part of the value for the view's `class`
attribute. These properties can be computed properties:
```javascript
MyView = Ember.View.extend({
classNameBindings: ['propertyA', 'propertyB'],
propertyA: 'from-a',
propertyB: function() {
if (someLogic) { return 'from-b'; }
}.property()
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view from-a from-b"></div>
```
If the value of a class name binding returns a boolean the property name
itself will be used as the class name if the property is true. The class name
will not be added if the value is `false` or `undefined`.
```javascript
MyView = Ember.View.extend({
classNameBindings: ['hovered'],
hovered: true
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view hovered"></div>
```
When using boolean class name bindings you can supply a string value other
than the property name for use as the `class` HTML attribute by appending the
preferred value after a ":" character when defining the binding:
```javascript
MyView = Ember.View.extend({
classNameBindings: ['awesome:so-very-cool'],
awesome: true
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view so-very-cool"></div>
```
Boolean value class name bindings whose property names are in a
camelCase-style format will be converted to a dasherized format:
```javascript
MyView = Ember.View.extend({
classNameBindings: ['isUrgent'],
isUrgent: true
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view is-urgent"></div>
```
Class name bindings can also refer to object values that are found by
traversing a path relative to the view itself:
```javascript
MyView = Ember.View.extend({
classNameBindings: ['messages.empty']
messages: Ember.Object.create({
empty: true
})
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view empty"></div>
```
If you want to add a class name for a property which evaluates to true and
and a different class name if it evaluates to false, you can pass a binding
like this:
```javascript
// Applies 'enabled' class when isEnabled is true and 'disabled' when isEnabled is false
Ember.View.extend({
classNameBindings: ['isEnabled:enabled:disabled']
isEnabled: true
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view enabled"></div>
```
When isEnabled is `false`, the resulting HTML representation looks like
this:
```html
<div id="ember1" class="ember-view disabled"></div>
```
This syntax offers the convenience to add a class if a property is `false`:
```javascript
// Applies no class when isEnabled is true and class 'disabled' when isEnabled is false
Ember.View.extend({
classNameBindings: ['isEnabled::disabled']
isEnabled: true
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view"></div>
```
When the `isEnabled` property on the view is set to `false`, it will result
in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view disabled"></div>
```
Updates to the the value of a class name binding will result in automatic
update of the HTML `class` attribute in the view's rendered HTML
representation. If the value becomes `false` or `undefined` the class name
will be removed.
Both `classNames` and `classNameBindings` are concatenated properties. See
[Ember.Object](/api/classes/Ember.Object.html) documentation for more
information about concatenated properties.
## HTML Attributes
The HTML attribute section of a view's tag can be set by providing an
`attributeBindings` property set to an array of property names on the view.
The return value of these properties will be used as the value of the view's
HTML associated attribute:
```javascript
AnchorView = Ember.View.extend({
tagName: 'a',
attributeBindings: ['href'],
href: 'http://google.com'
});
```
Will result in view instances with an HTML representation of:
```html
<a id="ember1" class="ember-view" href="http://google.com"></a>
```
One property can be mapped on to another by placing a ":" between
the source property and the destination property:
```javascript
AnchorView = Ember.View.extend({
tagName: 'a',
attributeBindings: ['url:href'],
url: 'http://google.com'
});
```
Will result in view instances with an HTML representation of:
```html
<a id="ember1" class="ember-view" href="http://google.com"></a>
```
If the return value of an `attributeBindings` monitored property is a boolean
the property will follow HTML's pattern of repeating the attribute's name as
its value:
```javascript
MyTextInput = Ember.View.extend({
tagName: 'input',
attributeBindings: ['disabled'],
disabled: true
});
```
Will result in view instances with an HTML representation of:
```html
<input id="ember1" class="ember-view" disabled="disabled" />
```
`attributeBindings` can refer to computed properties:
```javascript
MyTextInput = Ember.View.extend({
tagName: 'input',
attributeBindings: ['disabled'],
disabled: function() {
if (someLogic) {
return true;
} else {
return false;
}
}.property()
});
```
Updates to the the property of an attribute binding will result in automatic
update of the HTML attribute in the view's rendered HTML representation.
`attributeBindings` is a concatenated property. See [Ember.Object](/api/classes/Ember.Object.html)
documentation for more information about concatenated properties.
## Templates
The HTML contents of a view's rendered representation are determined by its
template. Templates can be any function that accepts an optional context
parameter and returns a string of HTML that will be inserted within the
view's tag. Most typically in Ember this function will be a compiled
`Ember.Handlebars` template.
```javascript
AView = Ember.View.extend({
template: Ember.Handlebars.compile('I am the template')
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view">I am the template</div>
```
Within an Ember application is more common to define a Handlebars templates as
part of a page:
```html
<script type='text/x-handlebars' data-template-name='some-template'>
Hello
</script>
```
And associate it by name using a view's `templateName` property:
```javascript
AView = Ember.View.extend({
templateName: 'some-template'
});
```
If you have nested resources, your Handlebars template will look like this:
```html
<script type='text/x-handlebars' data-template-name='posts/new'>
<h1>New Post</h1>
</script>
```
And `templateName` property:
```javascript
AView = Ember.View.extend({
templateName: 'posts/new'
});
```
Using a value for `templateName` that does not have a Handlebars template
with a matching `data-template-name` attribute will throw an error.
For views classes that may have a template later defined (e.g. as the block
portion of a `{{view}}` Handlebars helper call in another template or in
a subclass), you can provide a `defaultTemplate` property set to compiled
template function. If a template is not later provided for the view instance
the `defaultTemplate` value will be used:
```javascript
AView = Ember.View.extend({
defaultTemplate: Ember.Handlebars.compile('I was the default'),
template: null,
templateName: null
});
```
Will result in instances with an HTML representation of:
```html
<div id="ember1" class="ember-view">I was the default</div>
```
If a `template` or `templateName` is provided it will take precedence over
`defaultTemplate`:
```javascript
AView = Ember.View.extend({
defaultTemplate: Ember.Handlebars.compile('I was the default')
});
aView = AView.create({
template: Ember.Handlebars.compile('I was the template, not default')
});
```
Will result in the following HTML representation when rendered:
```html
<div id="ember1" class="ember-view">I was the template, not default</div>
```
## View Context
The default context of the compiled template is the view's controller:
```javascript
AView = Ember.View.extend({
template: Ember.Handlebars.compile('Hello {{excitedGreeting}}')
});
aController = Ember.Object.create({
firstName: 'Barry',
excitedGreeting: function() {
return this.get("content.firstName") + "!!!"
}.property()
});
aView = AView.create({
controller: aController
});
```
Will result in an HTML representation of:
```html
<div id="ember1" class="ember-view">Hello Barry!!!</div>
```
A context can also be explicitly supplied through the view's `context`
property. If the view has neither `context` nor `controller` properties, the
`parentView`'s context will be used.
## Layouts
Views can have a secondary template that wraps their main template. Like
primary templates, layouts can be any function that accepts an optional
context parameter and returns a string of HTML that will be inserted inside
view's tag. Views whose HTML element is self closing (e.g. `<input />`)
cannot have a layout and this property will be ignored.
Most typically in Ember a layout will be a compiled `Ember.Handlebars`
template.
A view's layout can be set directly with the `layout` property or reference
an existing Handlebars template by name with the `layoutName` property.
A template used as a layout must contain a single use of the Handlebars
`{{yield}}` helper. The HTML contents of a view's rendered `template` will be
inserted at this location:
```javascript
AViewWithLayout = Ember.View.extend({
layout: Ember.Handlebars.compile("<div class='my-decorative-class'>{{yield}}</div>"),
template: Ember.Handlebars.compile("I got wrapped")
});
```
Will result in view instances with an HTML representation of:
```html
<div id="ember1" class="ember-view">
<div class="my-decorative-class">
I got wrapped
</div>
</div>
```
See [Ember.Handlebars.helpers.yield](/api/classes/Ember.Handlebars.helpers.html#method_yield)
for more information.
## Responding to Browser Events
Views can respond to user-initiated events in one of three ways: method
implementation, through an event manager, and through `{{action}}` helper use
in their template or layout.
### Method Implementation
Views can respond to user-initiated events by implementing a method that
matches the event name. A `jQuery.Event` object will be passed as the
argument to this method.
```javascript
AView = Ember.View.extend({
click: function(event) {
// will be called when when an instance's
// rendered element is clicked
}
});
```
### Event Managers
Views can define an object as their `eventManager` property. This object can
then implement methods that match the desired event names. Matching events
that occur on the view's rendered HTML or the rendered HTML of any of its DOM
descendants will trigger this method. A `jQuery.Event` object will be passed
as the first argument to the method and an `Ember.View` object as the
second. The `Ember.View` will be the view whose rendered HTML was interacted
with. This may be the view with the `eventManager` property or one of its
descendant views.
```javascript
AView = Ember.View.extend({
eventManager: Ember.Object.create({
doubleClick: function(event, view) {
// will be called when when an instance's
// rendered element or any rendering
// of this view's descendant
// elements is clicked
}
})
});
```
An event defined for an event manager takes precedence over events of the
same name handled through methods on the view.
```javascript
AView = Ember.View.extend({
mouseEnter: function(event) {
// will never trigger.
},
eventManager: Ember.Object.create({
mouseEnter: function(event, view) {
// takes precedence over AView#mouseEnter
}
})
});
```
Similarly a view's event manager will take precedence for events of any views
rendered as a descendant. A method name that matches an event name will not
be called if the view instance was rendered inside the HTML representation of
a view that has an `eventManager` property defined that handles events of the
name. Events not handled by the event manager will still trigger method calls
on the descendant.
```javascript
var App = Ember.Application.create();
App.OuterView = Ember.View.extend({
template: Ember.Handlebars.compile("outer {{#view 'inner'}}inner{{/view}} outer"),
eventManager: Ember.Object.create({
mouseEnter: function(event, view) {
// view might be instance of either
// OuterView or InnerView depending on
// where on the page the user interaction occurred
}
})
});
App.InnerView = Ember.View.extend({
click: function(event) {
// will be called if rendered inside
// an OuterView because OuterView's
// eventManager doesn't handle click events
},
mouseEnter: function(event) {
// will never be called if rendered inside
// an OuterView.
}
});
```
### Handlebars `{{action}}` Helper
See [Handlebars.helpers.action](/api/classes/Ember.Handlebars.helpers.html#method_action).
### Event Names
All of the event handling approaches described above respond to the same set
of events. The names of the built-in events are listed below. (The hash of
built-in events exists in `Ember.EventDispatcher`.) Additional, custom events
can be registered by using `Ember.Application.customEvents`.
Touch events:
* `touchStart`
* `touchMove`
* `touchEnd`
* `touchCancel`
Keyboard events
* `keyDown`
* `keyUp`
* `keyPress`
Mouse events
* `mouseDown`
* `mouseUp`
* `contextMenu`
* `click`
* `doubleClick`
* `mouseMove`
* `focusIn`
* `focusOut`
* `mouseEnter`
* `mouseLeave`
Form events:
* `submit`
* `change`
* `focusIn`
* `focusOut`
* `input`
HTML5 drag and drop events:
* `dragStart`
* `drag`
* `dragEnter`
* `dragLeave`
* `dragOver`
* `dragEnd`
* `drop`
## Handlebars `{{view}}` Helper
Other `Ember.View` instances can be included as part of a view's template by
using the `{{view}}` Handlebars helper. See [Ember.Handlebars.helpers.view](/api/classes/Ember.Handlebars.helpers.html#method_view)
for additional information.
@class View
@namespace Ember
@extends Ember.CoreView
*/
var View = CoreView.extend({
concatenatedProperties: ['classNames', 'classNameBindings', 'attributeBindings'],
/**
@property isView
@type Boolean
@default true
@static
*/
isView: true,
// ..........................................................
// TEMPLATE SUPPORT
//
/**
The name of the template to lookup if no template is provided.
By default `Ember.View` will lookup a template with this name in
`Ember.TEMPLATES` (a shared global object).
@property templateName
@type String
@default null
*/
templateName: null,
/**
The name of the layout to lookup if no layout is provided.
By default `Ember.View` will lookup a template with this name in
`Ember.TEMPLATES` (a shared global object).
@property layoutName
@type String
@default null
*/
layoutName: null,
/**
Used to identify this view during debugging
@property instrumentDisplay
@type String
*/
instrumentDisplay: computed(function() {
if (this.helperName) {
return '{{' + this.helperName + '}}';
}
}),
/**
The template used to render the view. This should be a function that
accepts an optional context parameter and returns a string of HTML that
will be inserted into the DOM relative to its parent view.
In general, you should set the `templateName` property instead of setting
the template yourself.
@property template
@type Function
*/
template: computed('templateName', function(key, value) {
if (value !== undefined) { return value; }
var templateName = get(this, 'templateName');
var template = this.templateForName(templateName, 'template');
Ember.assert("You specified the templateName " + templateName + " for " + this + ", but it did not exist.", !templateName || !!template);
return template || get(this, 'defaultTemplate');
}),
_controller: null,
/**
The controller managing this view. If this property is set, it will be
made available for use by the template.
@property controller
@type Object
*/
controller: computed(function(key, value) {
if (arguments.length === 2) {
this._controller = value;
return value;
}
if (this._controller) {
return this._controller;
}
var parentView = this._parentView;
return parentView ? get(parentView, 'controller') : null;
}),
/**
A view may contain a layout. A layout is a regular template but
supersedes the `template` property during rendering. It is the
responsibility of the layout template to retrieve the `template`
property from the view (or alternatively, call `Handlebars.helpers.yield`,
`{{yield}}`) to render it in the correct location.
This is useful for a view that has a shared wrapper, but which delegates
the rendering of the contents of the wrapper to the `template` property
on a subclass.
@property layout
@type Function
*/
layout: computed(function(key) {
var layoutName = get(this, 'layoutName');
var layout = this.templateForName(layoutName, 'layout');
Ember.assert("You specified the layoutName " + layoutName + " for " + this + ", but it did not exist.", !layoutName || !!layout);
return layout || get(this, 'defaultLayout');
}).property('layoutName'),
_yield: function(context, options, morph) {
var template = get(this, 'template');
if (template) {
var useHTMLBars = false;
if (Ember.FEATURES.isEnabled('ember-htmlbars')) {
useHTMLBars = template.isHTMLBars;
}
if (useHTMLBars) {
return template.render(this, options, morph.contextualElement);
} else {
return template(context, options);
}
}
},
_blockArguments: EMPTY_ARRAY,
templateForName: function(name, type) {
if (!name) { return; }
Ember.assert("templateNames are not allowed to contain periods: "+name, name.indexOf('.') === -1);
if (!this.container) {
throw new EmberError('Container was not found when looking up a views template. ' +
'This is most likely due to manually instantiating an Ember.View. ' +
'See: http://git.io/EKPpnA');
}
return this.container.lookup('template:' + name);
},
/**
The object from which templates should access properties.
This object will be passed to the template function each time the render
method is called, but it is up to the individual function to decide what
to do with it.
By default, this will be the view's controller.
@property context
@type Object
*/
context: computed(function(key, value) {
if (arguments.length === 2) {
set(this, '_context', value);
return value;
} else {
return get(this, '_context');
}
}).volatile(),
/**
Private copy of the view's template context. This can be set directly
by Handlebars without triggering the observer that causes the view
to be re-rendered.
The context of a view is looked up as follows:
1. Supplied context (usually by Handlebars)
2. Specified controller
3. `parentView`'s context (for a child of a ContainerView)
The code in Handlebars that overrides the `_context` property first
checks to see whether the view has a specified controller. This is
something of a hack and should be revisited.
@property _context
@private
*/
_context: computed(function(key, value) {
if (arguments.length === 2) {
return value;
}
var parentView, controller;
if (controller = get(this, 'controller')) {
return controller;
}
parentView = this._parentView;
if (parentView) {
return get(parentView, '_context');
}
return null;
}),
/**
If a value that affects template rendering changes, the view should be
re-rendered to reflect the new value.
@method _contextDidChange
@private
*/
_contextDidChange: observer('context', function() {
this.rerender();
}),
/**
If `false`, the view will appear hidden in DOM.
@property isVisible
@type Boolean
@default null
*/
isVisible: true,
/**
Array of child views. You should never edit this array directly.
Instead, use `appendChild` and `removeFromParent`.
@property childViews
@type Array
@default []
@private
*/
childViews: childViewsProperty,
_childViews: EMPTY_ARRAY,
// When it's a virtual view, we need to notify the parent that their
// childViews will change.
_childViewsWillChange: beforeObserver('childViews', function() {
if (this.isVirtual) {
var parentView = get(this, 'parentView');
if (parentView) { propertyWillChange(parentView, 'childViews'); }
}
}),
// When it's a virtual view, we need to notify the parent that their
// childViews did change.
_childViewsDidChange: observer('childViews', function() {
if (this.isVirtual) {
var parentView = get(this, 'parentView');
if (parentView) { propertyDidChange(parentView, 'childViews'); }
}
}),
/**
Return the nearest ancestor that is an instance of the provided
class.
@method nearestInstanceOf
@param {Class} klass Subclass of Ember.View (or Ember.View itself)
@return Ember.View
@deprecated
*/
nearestInstanceOf: function(klass) {
Ember.deprecate("nearestInstanceOf is deprecated and will be removed from future releases. Use nearestOfType.");
var view = get(this, 'parentView');
while (view) {
if (view instanceof klass) { return view; }
view = get(view, 'parentView');
}
},
/**
Return the nearest ancestor that is an instance of the provided
class or mixin.
@method nearestOfType
@param {Class,Mixin} klass Subclass of Ember.View (or Ember.View itself),
or an instance of Ember.Mixin.
@return Ember.View
*/
nearestOfType: function(klass) {
var view = get(this, 'parentView');
var isOfType = klass instanceof Mixin ?
function(view) { return klass.detect(view); } :
function(view) { return klass.detect(view.constructor); };
while (view) {
if (isOfType(view)) { return view; }
view = get(view, 'parentView');
}