Skip to content

[Do Not Merge]fix(list-view): Recycle items for component children #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 10 additions & 1 deletion nativescript-angular/directives/list-view-comp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { ListView } from "tns-core-modules/ui/list-view";
import { View, KeyedTemplate } from "tns-core-modules/ui/core/view";
import { ObservableArray } from "tns-core-modules/data/observable-array";
import { LayoutBase } from "tns-core-modules/ui/layouts/layout-base";
import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout";
import { ProxyViewContainer } from "tns-core-modules/ui/proxy-view-container";
import { listViewLog } from "../trace";

const NG_VIEW = "_ngViewRef";
Expand Down Expand Up @@ -160,6 +162,7 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {

if (args.view && args.view[NG_VIEW]) {
listViewLog("onItemLoading: " + index + " - Reusing existing view");
console.log("onItemLoading: " + index + " - Reusing existing view");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug console.log leftover?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the code that should extract the view ref, event if the root view was "wrapped" in Stack layout

viewRef = args.view[NG_VIEW];
// getting angular view from original element (in cases when ProxyViewContainer
// is used NativeScript internally wraps it in a StackLayout)
Expand All @@ -169,6 +172,7 @@ export class ListViewComponent implements DoCheck, OnDestroy, AfterContentInit {
}
} else {
listViewLog("onItemLoading: " + index + " - Creating view from template");
console.log("onItemLoading: " + index + " - Creating view from template");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug console.log leftover?

viewRef = this.loader.createEmbeddedView(this.itemTemplate, new ListItemContext(), 0);
args.view = getItemViewRoot(viewRef);
args.view[NG_VIEW] = viewRef;
Expand Down Expand Up @@ -242,7 +246,12 @@ export interface ComponentView {
export type RootLocator = (nodes: Array<any>, nestLevel: number) => View;

export function getItemViewRoot(viewRef: ComponentView, rootLocator: RootLocator = getSingleViewRecursive): View {
const rootView = rootLocator(viewRef.rootNodes, 0);
let rootView = rootLocator(viewRef.rootNodes, -1);
if (rootView instanceof ProxyViewContainer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more borad check should be used:
if (!args.view instanceof LayoutBase || args.view instanceof ProxyViewContainer)
This will handle cases when template produces a single Label or Button for example

const wrapperLayout = new StackLayout();
wrapperLayout.addChild(rootView);
rootView = wrapperLayout;
}
return rootView;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/app/tests/list-view-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Component, Input, AfterViewInit } from "@angular/core";
import { TestApp } from "./test-app";
import { RootLocator, ComponentView, getItemViewRoot } from "nativescript-angular/directives/list-view-comp";
import { ProxyViewContainer } from "tns-core-modules/ui/proxy-view-container";
import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout";
import { Label } from "tns-core-modules/ui/label";

// import trace = require("trace");
// trace.setCategories("ns-list-view, " + trace.categories.Navigation);
Expand Down Expand Up @@ -121,3 +123,25 @@ describe("ListView-tests", () => {
.catch(done);
});
});

describe("ListView item templates", () => {
it("wraps components in StackLayout", () => {
const view: ComponentView = {
rootNodes: [],
destroy: () => {}
};
const childRoot = new ProxyViewContainer();
const itemRoot = getItemViewRoot(view, (_rootNodes, _level) => childRoot);
assert.isTrue(itemRoot instanceof StackLayout, "ProxyViewContainer wrapped in StackLayout");
});

it("does not wrap non-component children", () => {
const view: ComponentView = {
rootNodes: [],
destroy: () => {}
};
const childRoot = new Label();
const itemRoot = getItemViewRoot(view, (_rootNodes, _level) => childRoot);
assert.isTrue(itemRoot instanceof Label, "'normal' children not wrapped");
});
});