1 | import {inject} from 'aurelia-dependency-injection';
|
2 | import {BoundViewFactory, ViewSlot, customAttribute, templateController, ViewFactory} from 'aurelia-templating';
|
3 | import {createOverrideContext} from 'aurelia-binding';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | @customAttribute('with')
|
9 | @templateController
|
10 | @inject(BoundViewFactory, ViewSlot)
|
11 | export class With {
|
12 |
|
13 |
|
14 | viewFactory: any;
|
15 |
|
16 | viewSlot: any;
|
17 |
|
18 | parentOverrideContext: any;
|
19 |
|
20 | view: any;
|
21 |
|
22 | value: any;
|
23 |
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 | constructor(viewFactory: ViewFactory, viewSlot: ViewSlot) {
|
30 | this.viewFactory = viewFactory;
|
31 | this.viewSlot = viewSlot;
|
32 | this.parentOverrideContext = null;
|
33 | this.view = null;
|
34 | }
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | bind(bindingContext, overrideContext) {
|
42 | this.parentOverrideContext = overrideContext;
|
43 | this.valueChanged(this.value);
|
44 | }
|
45 |
|
46 | |
47 |
|
48 |
|
49 |
|
50 | valueChanged(newValue) {
|
51 | let overrideContext = createOverrideContext(newValue, this.parentOverrideContext);
|
52 | let view = this.view;
|
53 | if (!view) {
|
54 | view = this.view = this.viewFactory.create();
|
55 | view.bind(newValue, overrideContext);
|
56 | this.viewSlot.add(view);
|
57 | } else {
|
58 | view.bind(newValue, overrideContext);
|
59 | }
|
60 | }
|
61 |
|
62 | |
63 |
|
64 |
|
65 | unbind() {
|
66 | let view = this.view;
|
67 | this.parentOverrideContext = null;
|
68 |
|
69 | if (view) {
|
70 | view.unbind();
|
71 | }
|
72 | }
|
73 | }
|