Skip to content

Commit 986cf41

Browse files
committed
Merge pull request NativeScript#1184 from NativeScript/cankov/context-to-template
Propagate context to template Views
2 parents 72577a8 + af34329 commit 986cf41

File tree

6 files changed

+40
-1
lines changed

6 files changed

+40
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Our unit test will set this function and expect it to be set as a handler on a View in the XML.
2+
export var test: (args: any) => void;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
2+
xmlns:tc="xml-declaration/template-builder-tests/template-view">
3+
<tc:TemplateView id="template-view">
4+
<tc:TemplateView.template>
5+
<tc:TemplateView text="Click!" test="test" />
6+
</tc:TemplateView.template>
7+
</tc:TemplateView>
8+
</Page>

apps/tests/xml-declaration/template-builder-tests/template-view.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export class TemplateView extends LayoutBase {
1818
null
1919
)
2020
);
21+
22+
public static testEvent: string = "test";
2123

2224
get template(): string | Template {
2325
return this._getValue(TemplateView.templateProperty);

apps/tests/xml-declaration/xml-declaration-tests.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,4 +880,30 @@ export function test_NonExistingElementInTemplateError() {
880880
message = e.message;
881881
}
882882
TKUnit.assertEqual(message.substr(0, expectedErrorStart.length), expectedErrorStart, "Expected load to throw, and the message to start with specific string");
883+
}
884+
885+
export function test_EventInTemplate() {
886+
var pageCode = require("./template-builder-tests/event-in-template");
887+
888+
var notified = false;
889+
pageCode.test = (args) => {
890+
notified = true;
891+
}
892+
893+
var basePath = "xml-declaration/";
894+
var message;
895+
var page = builder.load(__dirname + "/template-builder-tests/event-in-template.xml", pageCode);
896+
TKUnit.assert(view, "Expected the xml to generate a page");
897+
var templateView = <TemplateView>page.getViewById("template-view");
898+
TKUnit.assert(templateView, "Expected the page to have a TemplateView with 'temaplte-view' id.");
899+
templateView.parseTemplate();
900+
TKUnit.assertEqual(templateView.getChildrenCount(), 1, "Expected TemplateView initially to have 1 child.");
901+
var childTemplateView = <TemplateView>templateView.getChildAt(0);
902+
TKUnit.assert(childTemplateView, "Expected the TemplateView's template to create a child TemplateView.");
903+
childTemplateView.notify({
904+
eventName: "test",
905+
object: childTemplateView
906+
});
907+
908+
TKUnit.assert(notified, "Expected the child to raise the test event.");
883909
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@
319319
"apps/tests/xml-declaration/mainPage.ts",
320320
"apps/tests/xml-declaration/mymodule/MyControl.ts",
321321
"apps/tests/xml-declaration/mymodulewithxml/MyControl.ts",
322+
"apps/tests/xml-declaration/template-builder-tests/event-in-template.ts",
322323
"apps/tests/xml-declaration/template-builder-tests/template-view.ts",
323324
"apps/tests/xml-declaration/xml-declaration-tests.ts",
324325
"apps/tests/xml-parser-tests/xml-parser-tests.ts",

ui/builder/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ namespace xml2ui {
438438

439439
if (ComponentParser.isKnownTemplate(name, parent.exports)) {
440440
return new TemplateParser(this, {
441-
context: parent ? getExports(parent.component) : null, // Passing 'context' won't work if you set "codeFile" on the page
441+
context: (parent ? getExports(parent.component) : null) || this.context, // Passing 'context' won't work if you set "codeFile" on the page
442442
parent: parent,
443443
name: name,
444444
elementName: args.elementName,

0 commit comments

Comments
 (0)