Skip to content

Commit

Permalink
feat(with): port the with template controller from vCurrent
Browse files Browse the repository at this point in the history
  • Loading branch information
EisenbergEffect committed Apr 24, 2018
1 parent 1aa6d5b commit 4b39aae
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ An experimental re-working of Aurelia, oriented around compile-time reflection a
* [x] `if`
* [x] `else`
* [x] `replaceable`
* [x] `with`
* [ ] `repeat`
* Custom Elements
* [x] `compose`
Expand Down
84 changes: 64 additions & 20 deletions scripts/app-bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/app-bundle.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/runtime/configuration/standard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Compose } from "../resources/compose";
import { SelfBindingBehavior } from "../resources/self-binding-behavior";
import { ThrottleBindingBehavior } from "../resources/throttle-binding-behavior";
import { UpdateTriggerBindingBehavior } from "../resources/update-trigger-binding-behavior";
import { With } from "../resources/with";

export const StandardConfiguration = {
register(container: IContainer) {
Expand All @@ -37,6 +38,7 @@ export const StandardConfiguration = {
If,
Else,
Replaceable,
With,

// Custom Elements
Compose
Expand Down
22 changes: 11 additions & 11 deletions src/runtime/resources/if-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ import { IScope } from '../binding/binding-context';
* For internal use only. May change without warning.
*/
export abstract class IfCore {
private visual: IVisual = null;
private child: IVisual = null;
private $scope: IScope = null;

// If the child view is animated, `condition` might not reflect the internal
// state anymore, so we use `showing` for that.
// Eventually, `showing` and `condition` should be consistent.
protected showing = false;

constructor(private viewFactory: IViewFactory, protected viewSlot: IViewSlot) { }
constructor(private factory: IViewFactory, protected viewSlot: IViewSlot) { }

unbound() {
const visual = this.visual;
const visual = this.child;

if (visual === null) {
return;
}

this.visual.unbind();
this.child.unbind();

if (!this.viewFactory.isCaching) {
if (!this.factory.isCaching) {
return;
}

Expand All @@ -36,19 +36,19 @@ export abstract class IfCore {
visual.tryReturnToCache();
}

this.visual = null;
this.child = null;
}

show() {
if (this.visual === null) {
this.visual = this.viewFactory.create();
if (this.child === null) {
this.child = this.factory.create();
}

this.visual.bind(this.$scope);
this.child.bind(this.$scope);

if (!this.showing) {
this.showing = true;
return this.viewSlot.add(this.visual);
return this.viewSlot.add(this.child);
}
}

Expand All @@ -57,7 +57,7 @@ export abstract class IfCore {
return;
}

const visual = this.visual;
const visual = this.child;
const removed = this.viewSlot.remove(visual);

this.showing = false;
Expand Down
12 changes: 6 additions & 6 deletions src/runtime/resources/replaceable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import { IScope } from '../binding/binding-context';
@templateController
@inject(IViewFactory, IViewSlot)
export class Replaceable {
private visual: IVisual;
private child: IVisual;

constructor(private viewFactory: IViewFactory, private viewSlot: IViewSlot) {
this.visual = this.viewFactory.create();
this.viewSlot.add(this.visual);
constructor(private factory: IViewFactory, private viewSlot: IViewSlot) {
this.child = this.factory.create();
this.viewSlot.add(this.child);
}

bound(scope: IScope) {
this.visual.bind(scope);
this.child.bind(scope);
}

unbound() {
this.visual.unbind();
this.child.unbind();
}
}
35 changes: 35 additions & 0 deletions src/runtime/resources/with.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { customAttribute, templateController, inject } from "../decorators";
import { IViewFactory, IVisual } from "../templating/view-engine";
import { IViewSlot } from "../templating/view-slot";
import { IScope, BindingContext } from "../binding/binding-context";

@customAttribute('with')
@templateController
@inject(IViewFactory, IViewSlot)
export class With {
//#region Framework-Supplied
private $scope: IScope;
//#endregion

private child: IVisual = null;

value: any = null;

constructor(private factory: IViewFactory, private viewSlot: IViewSlot) {
this.child = factory.create();
this.viewSlot.add(this.child);
}

valueChanged(newValue: any) {
const childScope = {
bindingContext: newValue,
overrideContext: BindingContext.createOverride(newValue, this.$scope.overrideContext)
};

this.child.bind(childScope);
}

unbound() {
this.child.unbind();
}
}

0 comments on commit 4b39aae

Please sign in to comment.