Skip to content
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

Fix dxi component template rerendering issues #560

Merged
merged 8 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,28 @@ export class AppComponent {
}
```

Note, in some scenarios an item content can be rerendered by it's host component. To keep the item child components alive they should be declared as a single template via the `dxTemplate` structural directive. There are several alternatives depending on the item content:
Copy link
Contributor

@kvet kvet Sep 21, 2017

Choose a reason for hiding this comment

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

How can I find out is my scenario suited a described one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. One way is if something works unexpectedly. Would you deprecate the approach without using dxTemplate in dxi-* components? Or any other thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

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

So, the workflow:

  • write code
  • check that it works correctly in all cases (if it embedded in another component and so on)
  • if something went wrong - add dxTemplate and check again

It's a weird solution. It will be better to mark dxi- components without dxTemplate as deprecated and buggy in order to suppress support traffic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It appears that simple transclusion is much faster than using templates. So I'm afraid we shouldn't deprecate this approach. A clear condition for using templates is "you item content has any nested components (not just static markup)".


```html
<dx-list>
<dxi-item>
<dx-button text="One Child" *dxTemplate></dx-button>
</dxi-item>
<dxi-item>
<div *dxTemplate>
<dx-button text="First Child"></dx-button>
<dx-button text="Second Child"></dx-button>
</div>
</dxi-item>
<dxi-item>
<ng-template dxTemplate>
<dx-button text="First Child"></dx-button>
<dx-button text="Second Child"></dx-button>
</ng-template>
</dxi-item>
</dx-list>
```

Angular has a built-in `template` directive. To define the `template` property of the configuration component (for example, `dxo-master-detail`), use the following code:
```html
<dxo-master-detail [template]="'masterDetail'"></dxo-master-detail>
Expand Down
5 changes: 3 additions & 2 deletions src/core/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@angular/core';

import { DxTemplateDirective } from './template';
import { DxTemplateHost } from './template-host';
import { IDxTemplateHost, DxTemplateHost } from './template-host';
import { EmitterHelper } from './events-strategy';
import { WatcherHelper } from './watcher-helper';
import {
Expand All @@ -17,7 +17,8 @@ import {
CollectionNestedOptionContainerImpl
} from './nested-option';

export abstract class DxComponent implements AfterViewInit, AfterContentChecked, INestedOptionContainer, ICollectionNestedOptionContainer {
export abstract class DxComponent implements AfterViewInit, AfterContentChecked,
INestedOptionContainer, ICollectionNestedOptionContainer, IDxTemplateHost {
private _optionToUpdate: any = {};
private _collectionContainerImpl: ICollectionNestedOptionContainer;
eventHelper: EmitterHelper;
Expand Down
9 changes: 6 additions & 3 deletions src/core/template-host.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { DxComponent } from './component';
import { DxTemplateDirective } from './template';

export interface IDxTemplateHost {
Copy link
Contributor

@kvet kvet Sep 21, 2017

Choose a reason for hiding this comment

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

We don't use I prefix for interfaces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

5 of 7 existing interfaces have I prefix and just 2 don't. I would add it to the rest two.

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are good points why we should use the I prefix :)

setTemplate(template: DxTemplateDirective);
};

export class DxTemplateHost {
host: DxComponent;
setHost(host: DxComponent) {
host: IDxTemplateHost;
setHost(host: IDxTemplateHost) {
this.host = host;
}
setTemplate(template: DxTemplateDirective) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class RenderData {
}

@Directive({
selector: '[dxTemplate][dxTemplateOf]'
selector: '[dxTemplate]'
})
export class DxTemplateDirective {
@Input()
Expand Down
20 changes: 14 additions & 6 deletions templates/nested-component.tst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import {
QueryList<#?#>
} from '@angular/core';

import { NestedOptionHost<#? it.hasTemplate #>, extractTemplate<#?#> } from '../../core/nested-option';
import { NestedOptionHost<#? it.hasTemplate #>, extractTemplate<#?#> } from '../../core/nested-option';<#? it.hasTemplate #>
import { DxTemplateDirective } from '../../core/template';
import { IDxTemplateHost, DxTemplateHost } from '../../core/template-host';<#?#>
import { <#= it.baseClass #> } from '<#= it.basePath #>';
<#~ it.collectionNestedComponents :component:i #><#? component.className !== it.className #>import { <#= component.className #>Component } from './<#= component.path #>';
<#?#><#~#>
Expand All @@ -22,12 +24,12 @@ import { <#= it.baseClass #> } from '<#= it.basePath #>';
selector: '<#= it.selector #>',
template: '<#? it.hasTemplate #><ng-content></ng-content><#?#>',
styles: ['<#? it.hasTemplate #>:host { display: block; }<#?#>'],
providers: [NestedOptionHost]<#? it.inputs #>,
providers: [NestedOptionHost<#? it.hasTemplate #>, DxTemplateHost<#?#>]<#? it.inputs #>,
inputs: [<#~ it.inputs :input:i #>
'<#= input.name #>'<#? i < it.inputs.length-1 #>,<#?#><#~#>
]<#?#>
})
export class <#= it.className #>Component extends <#= it.baseClass #><#? it.hasTemplate #> implements AfterViewInit<#?#> {<#~ it.properties :prop:i #>
export class <#= it.className #>Component extends <#= it.baseClass #><#? it.hasTemplate #> implements AfterViewInit, IDxTemplateHost<#?#> {<#~ it.properties :prop:i #>
@Input()
get <#= prop.name #>() {
return this._getOption('<#= prop.name #>');
Expand All @@ -49,13 +51,19 @@ export class <#= it.className #>Component extends <#= it.baseClass #><#? it.hasT
this.setChildren('<#= component.propertyName #>', value);
}
<#~#>

constructor(@SkipSelf() @Host() parentOptionHost: NestedOptionHost, @Host() optionHost: NestedOptionHost<#? it.hasTemplate #>, private element: ElementRef<#?#>) {
constructor(@SkipSelf() @Host() parentOptionHost: NestedOptionHost,
@Host() optionHost: NestedOptionHost<#? it.hasTemplate #>,
@Host() templateHost: DxTemplateHost,
private element: ElementRef<#?#>) {
super();
parentOptionHost.setNestedOption(this);
optionHost.setHost(this, this._fullOptionPath.bind(this));
optionHost.setHost(this, this._fullOptionPath.bind(this));<#? it.hasTemplate #>
templateHost.setHost(this);<#?#>
}
<#? it.hasTemplate #>
setTemplate(template: DxTemplateDirective) {
this.template = template;
}
ngAfterViewInit() {
extractTemplate(this, this.element);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/src/ui/list.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* tslint:disable:component-selector */

import {
VERSION,
Component,
ViewChildren,
QueryList,
Expand Down Expand Up @@ -515,4 +516,43 @@ describe('DxList', () => {

expect(fixture.componentInstance.buttonDestroyed).toBe(true);
});

it('should use item template to render/rerender an item with a template (T532675)', async(() => {
const ngTemplateName = Number(VERSION.major) >= 4 ? 'ng-template' : 'template';

TestBed.configureTestingModule({
declarations: [TestContainerComponent],
imports: [DxButtonModule, DxListModule]
});

TestBed.overrideComponent(TestContainerComponent, {
set: {
template: `
<dx-list>
<dxi-item>
<dx-button *dxTemplate></dx-button>
</dxi-item>
<dxi-item>
<${ngTemplateName} dxTemplate>
<dx-button></dx-button>
</${ngTemplateName}>
</dxi-item>
</dx-list>
`
}
});

let fixture = TestBed.createComponent(TestContainerComponent);
fixture.detectChanges();

let instance = getWidget(fixture);
expect(instance.element().find('.dx-button').eq(0).dxButton('instance')).not.toBeUndefined();
expect(instance.element().find('.dx-button').eq(1).dxButton('instance')).not.toBeUndefined();

instance.repaint();
fixture.detectChanges();
expect(instance.element().find('.dx-button').eq(0).dxButton('instance')).not.toBeUndefined();
expect(instance.element().find('.dx-button').eq(1).dxButton('instance')).not.toBeUndefined();
}));

});