Add Template factory function and use it in the Repeater and ListView components

This commit is contained in:
Panayot Cankov
2015-11-20 12:43:12 +02:00
parent 03e69cbf85
commit e95820b04a
13 changed files with 110 additions and 23 deletions

View File

@@ -0,0 +1,8 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:tc="xml-declaration/template-builder-tests/template-view">
<tc:TemplateView id="template-view">
<tc:TemplateView.template>
<Button text="Click!" />
</tc:TemplateView.template>
</tc:TemplateView>
</Page>

View File

@@ -0,0 +1,33 @@
import { View, Template } from "ui/core/view"
import { PropertyChangeData, Property, PropertyMetadataSettings } from "ui/core/dependency-observable"
import * as proxy from "ui/core/proxy"
import { LayoutBase } from "ui/layouts/layout-base"
import { parse } from "ui/builder"
export module knownTemplates {
export var template = "template";
}
export class TemplateView extends LayoutBase {
public static templateProperty = new Property(
"template",
"TemplateView",
new proxy.PropertyMetadata(
undefined,
PropertyMetadataSettings.AffectsLayout,
null
)
);
get template(): string | Template {
return this._getValue(TemplateView.templateProperty);
}
set template(value: string | Template) {
this._setValue(TemplateView.templateProperty, value);
}
public parseTemplate() {
this.addChild(parse(this.template));
}
}

View File

@@ -14,6 +14,8 @@ import stackLayoutModule = require("ui/layouts/stack-layout");
import {Label} from "ui/label";
import {Page} from "ui/page";
import {Button} from "ui/button";
import {View} from "ui/core/view";
import {TemplateView} from "./template-builder-tests/template-view";
import myCustomControlWithoutXml = require("./mymodule/MyControl");
import listViewModule = require("ui/list-view");
import helper = require("../ui/helper");
@@ -818,4 +820,19 @@ export function test_searchbar_donotcrash_whentext_isspace() {
var sb = <searchBarModule.SearchBar>p.content;
TKUnit.assertEqual(sb.text, " ");
};
};
export function test_parse_template_property() {
var page = <Page>builder.load(fs.path.join(__dirname, "template-builder-tests/simple-template-test.xml"));
TKUnit.assert(page, "Expected root page.");
var templateView = <TemplateView>page.getViewById("template-view");
TKUnit.assert(templateView, "Expected TemplateView.");
TKUnit.assert(templateView.template, "Expected the template of the TemplateView to be defined");
TKUnit.assertEqual(templateView.getChildrenCount(), 0, "Expected TemplateView initially to have no children.");
templateView.parseTemplate();
TKUnit.assertEqual(templateView.getChildrenCount(), 1, "Expected TemplateView initially to have 1 child.");
var button = <Button>templateView.getChildAt(0);
TKUnit.assert(button, "Expected the TemplateView's template to create a button child.");
TKUnit.assertEqual(button.text, "Click!", "Expected child Button to have text 'Click!'");
}