Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Bind functions passed to DNodes to the enclosing widget that they are declared #634

Merged
merged 2 commits into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions src/WidgetBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Map from '@dojo/shim/Map';
import '@dojo/shim/Promise'; // Imported for side-effects
import Set from '@dojo/shim/Set';
import WeakMap from '@dojo/shim/WeakMap';
import { isWNode, registry, v } from './d';
import { isWNode, registry, v, isHNode } from './d';
import { auto } from './diff';
import {
AfterRender,
Expand Down Expand Up @@ -368,6 +368,7 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
const render = this._runBeforeRenders();
let dNode = render();
dNode = this.runAfterRenders(dNode);
this._decorateNodes(dNode);
const widget = this._dNodeToVNode(dNode);
this._manageDetachedChildren();
this._cachedVNode = widget;
Expand All @@ -378,6 +379,27 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
return this._cachedVNode;
}

private _decorateNodes(node: DNode | DNode[]) {
let nodes = Array.isArray(node) ? [ ...node ] : [ node ];
while (nodes.length) {
const node = nodes.pop();
if (isHNode(node)) {
if (node.properties.key) {
node.properties.afterCreate = this._afterCreateCallback;
node.properties.afterUpdate = this._afterUpdateCallback;
}
}
else if (!isWNode(node)) {
continue;
}
node.properties = node.properties || {};
if ((<any> node.properties).bind === undefined) {
(<any> node.properties).bind = this;
}
nodes = [ ...nodes, ...node.children ];
}
}

protected invalidate(): void {
if (this._renderState === WidgetRenderState.IDLE) {
this._dirty = true;
Expand Down Expand Up @@ -599,10 +621,6 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
}
}

if ((<any> properties).bind === undefined) {
(<any> properties).bind = this;
}

if (cachedChild !== undefined) {
child = cachedChild.child;
child.__setProperties__(properties);
Expand Down Expand Up @@ -632,15 +650,6 @@ export class WidgetBase<P = WidgetProperties, C extends DNode = DNode> extends E
return child.__render__();
}

if (dNode.properties.key !== undefined) {
dNode.properties.afterCreate = this._afterCreateCallback;
dNode.properties.afterUpdate = this._afterUpdateCallback;
}

if (dNode.properties.bind === undefined) {
(<any> dNode).properties.bind = this;
}

dNode.vNodes = [];
for (let i = 0; i < dNode.children.length; i++) {
const child = dNode.children[i];
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/WidgetBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,9 +994,11 @@ registerSuite({
assert.strictEqual(result.children![1].properties.key, 'span');
},
'instance gets passed to VNodeProperties as bind to widget and all children'() {

class InnerWidget extends WidgetBase {}
class TestWidget extends WidgetBase<any> {
render() {
return v('div', [
return w(InnerWidget, {}, [
v('header', [
v('section')
])
Expand All @@ -1007,9 +1009,12 @@ registerSuite({
const widget: any = new TestWidget();
const result = <VNode> widget.__render__();
assert.lengthOf(result.children, 1);
assert.strictEqual(result.properties!.bind, widget);
assert.notStrictEqual(result.properties!.bind, widget);
assert.instanceOf(result.properties!.bind, InnerWidget);
assert.strictEqual(result.children![0].properties!.bind, widget);
assert.instanceOf(result.children![0].properties!.bind, TestWidget);
assert.strictEqual(result.children![0].children![0].properties!.bind, widget);
assert.instanceOf(result.children![0].children![0].properties!.bind, TestWidget);
},
'render with multiple text node children'() {
class TestWidget extends WidgetBase<any> {
Expand Down