diff --git a/apps/tests/xml-declaration/template-builder-tests/event-in-template.ts b/apps/tests/xml-declaration/template-builder-tests/event-in-template.ts new file mode 100644 index 000000000..7fc17f985 --- /dev/null +++ b/apps/tests/xml-declaration/template-builder-tests/event-in-template.ts @@ -0,0 +1,2 @@ +// Our unit test will set this function and expect it to be set as a handler on a View in the XML. +export var test: (args: any) => void; \ No newline at end of file diff --git a/apps/tests/xml-declaration/template-builder-tests/event-in-template.xml b/apps/tests/xml-declaration/template-builder-tests/event-in-template.xml new file mode 100644 index 000000000..239e6130d --- /dev/null +++ b/apps/tests/xml-declaration/template-builder-tests/event-in-template.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/apps/tests/xml-declaration/template-builder-tests/template-view.ts b/apps/tests/xml-declaration/template-builder-tests/template-view.ts index 43fcf1d89..5083d0602 100644 --- a/apps/tests/xml-declaration/template-builder-tests/template-view.ts +++ b/apps/tests/xml-declaration/template-builder-tests/template-view.ts @@ -18,6 +18,8 @@ export class TemplateView extends LayoutBase { null ) ); + + public static testEvent: string = "test"; get template(): string | Template { return this._getValue(TemplateView.templateProperty); diff --git a/apps/tests/xml-declaration/xml-declaration-tests.ts b/apps/tests/xml-declaration/xml-declaration-tests.ts index 08cb151cd..051ecc5c0 100644 --- a/apps/tests/xml-declaration/xml-declaration-tests.ts +++ b/apps/tests/xml-declaration/xml-declaration-tests.ts @@ -880,4 +880,30 @@ export function test_NonExistingElementInTemplateError() { message = e.message; } TKUnit.assertEqual(message.substr(0, expectedErrorStart.length), expectedErrorStart, "Expected load to throw, and the message to start with specific string"); +} + +export function test_EventInTemplate() { + var pageCode = require("./template-builder-tests/event-in-template"); + + var notified = false; + pageCode.test = (args) => { + notified = true; + } + + var basePath = "xml-declaration/"; + var message; + var page = builder.load(__dirname + "/template-builder-tests/event-in-template.xml", pageCode); + TKUnit.assert(view, "Expected the xml to generate a page"); + var templateView = page.getViewById("template-view"); + TKUnit.assert(templateView, "Expected the page to have a TemplateView with 'temaplte-view' id."); + templateView.parseTemplate(); + TKUnit.assertEqual(templateView.getChildrenCount(), 1, "Expected TemplateView initially to have 1 child."); + var childTemplateView = templateView.getChildAt(0); + TKUnit.assert(childTemplateView, "Expected the TemplateView's template to create a child TemplateView."); + childTemplateView.notify({ + eventName: "test", + object: childTemplateView + }); + + TKUnit.assert(notified, "Expected the child to raise the test event."); } \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 7464a795f..1279c1f30 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -319,6 +319,7 @@ "apps/tests/xml-declaration/mainPage.ts", "apps/tests/xml-declaration/mymodule/MyControl.ts", "apps/tests/xml-declaration/mymodulewithxml/MyControl.ts", + "apps/tests/xml-declaration/template-builder-tests/event-in-template.ts", "apps/tests/xml-declaration/template-builder-tests/template-view.ts", "apps/tests/xml-declaration/xml-declaration-tests.ts", "apps/tests/xml-parser-tests/xml-parser-tests.ts", diff --git a/ui/builder/builder.ts b/ui/builder/builder.ts index 3955e1b08..edae02b44 100644 --- a/ui/builder/builder.ts +++ b/ui/builder/builder.ts @@ -438,7 +438,7 @@ namespace xml2ui { if (ComponentParser.isKnownTemplate(name, parent.exports)) { return new TemplateParser(this, { - context: parent ? getExports(parent.component) : null, // Passing 'context' won't work if you set "codeFile" on the page + context: (parent ? getExports(parent.component) : null) || this.context, // Passing 'context' won't work if you set "codeFile" on the page parent: parent, name: name, elementName: args.elementName,