Skip to content

Commit e6f0440

Browse files
committed
Support NativeScript templates.
Extract the string value and set it to parent.itemTemplate.
1 parent c769c9d commit e6f0440

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Diff for: src/nativescript-angular/renderer.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {TemplateCloner} from 'angular2/src/render/dom/template_cloner';
1414
import {NG_BINDING_CLASS, cloneAndQueryProtoView} from 'angular2/src/render/dom/util';
1515
import {DOM} from 'angular2/src/dom/dom_adapter';
1616

17-
import {ViewNode} from 'nativescript-angular/view_node';
17+
import {ViewNode, DummyViewNode} from 'nativescript-angular/view_node';
1818

1919
export class NativeScriptView {
2020
public eventDispatcher: RenderEventDispatcher;
@@ -228,7 +228,18 @@ export class NativeScriptRenderer extends Renderer {
228228
parsedChildren.forEach(node => {
229229
var viewNode: ViewNode;
230230
if (node.type == "tag") {
231-
viewNode = new ViewNode(parent, node.name, node.attribs);
231+
if (node.name.indexOf('itemtemplate') >= 0) {
232+
// all lowercased. the only thing that can work here
233+
// is kebab-cased tags and attributes.
234+
let templateString = DOM.getInnerHTML(node);
235+
parent.setAttribute('itemTemplate', templateString);
236+
// return a view node even if we do nothing with it so that
237+
// index-based binding configuration doesn't break for bound
238+
// nodes coming after.
239+
viewNode = new DummyViewNode(parent);
240+
} else {
241+
viewNode = new ViewNode(parent, node.name, node.attribs);
242+
}
232243
} else if (node.type == "text") {
233244
//viewNode = new ViewNode(parent, "rawtext", {text: node.data});
234245
//Ignore text nodes

Diff for: src/nativescript-angular/view_node.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ export class ViewNode {
125125
(<any>this.parentNativeView)._addChildFromBuilder(this.viewName, this.nativeView);
126126
this.attachUIEvents();
127127
} else {
128-
throw new Error("Parent view can't have children! " + this._parentView);
128+
console.log('parentNativeView: ' + this.parentNativeView);
129+
throw new Error("Parent view can't have children! " + this.parentNativeView);
129130
}
130131
}
131132

@@ -159,6 +160,12 @@ export class ViewNode {
159160
}
160161

161162
public setAttribute(attributeName: string, value: any): void {
163+
if (!this.nativeView) {
164+
console.log('Native view not created. Delaying attribute set: ' + attributeName);
165+
this.attributes[attributeName] = value;
166+
return;
167+
}
168+
162169
console.log('Setting attribute: ' + attributeName);
163170

164171
let specialSetter = getSpecialPropertySetter(attributeName);
@@ -254,7 +261,7 @@ export class ViewNode {
254261
return this.children.indexOf(childNode);
255262
}
256263

257-
setProperty(name: string, value: any) {
264+
public setProperty(name: string, value: any) {
258265
console.log('ViewNode.setProperty ' + this.viewName + ' setProperty ' + name + ' ' + value);
259266
if (this.nativeView) {
260267
this.setAttribute(name, value);
@@ -287,3 +294,17 @@ export class ViewNode {
287294
}
288295

289296
}
297+
298+
export class DummyViewNode extends ViewNode {
299+
constructor(public parentNode: ViewNode) {
300+
super(parentNode, null, {});
301+
}
302+
public attachToView(atIndex: number = -1) {
303+
}
304+
public insertChildAt(index: number, childNode: ViewNode) {
305+
}
306+
public removeChild(childNode: ViewNode) {
307+
}
308+
setProperty(name: string, value: any) {
309+
}
310+
}

0 commit comments

Comments
 (0)