UNPKG

1.39 kBPlain TextView Raw
1import {inject} from 'aurelia-dependency-injection';
2import {BoundViewFactory, ViewSlot, customAttribute, templateController, ViewFactory, View} from 'aurelia-templating';
3
4/**
5 * Marks any part of a view to be replacable by the consumer.
6 */
7@customAttribute('replaceable')
8@templateController
9@inject(BoundViewFactory, ViewSlot)
10export class Replaceable {
11
12 /**@internal*/
13 viewFactory: ViewFactory;
14 /**@internal*/
15 viewSlot: ViewSlot;
16 /**@internal*/
17 view: View;
18
19 /**
20 * @param viewFactory target The factory generating the view.
21 * @param viewSlot viewSlot The slot the view is injected in to.
22 */
23 constructor(viewFactory: ViewFactory, viewSlot: ViewSlot) {
24 this.viewFactory = viewFactory; // This is referenced internally in the Controller's bind method.
25 this.viewSlot = viewSlot;
26 this.view = null;
27 }
28
29 /**
30 * Binds the replaceable to the binding context and override context.
31 * @param bindingContext The binding context.
32 * @param overrideContext An override context for binding.
33 */
34 bind(bindingContext, overrideContext) {
35 if (this.view === null) {
36 this.view = (this.viewFactory as any).create();
37 this.viewSlot.add(this.view);
38 }
39
40 this.view.bind(bindingContext, overrideContext);
41 }
42
43 /**
44 * Unbinds the replaceable.
45 */
46 unbind() {
47 this.view.unbind();
48 }
49}