mirror of https://github.com/abpframework/abp.git
committed by
GitHub
4 changed files with 19 additions and 26 deletions
@ -1,18 +1,17 @@ |
|||||
import { Injectable, TrackByFunction } from '@angular/core'; |
import { Injectable, TrackByFunction } from '@angular/core'; |
||||
|
import { O } from 'ts-toolbelt'; |
||||
|
|
||||
|
export const trackBy = <T = any>(key: keyof T): TrackByFunction<T> => (_, item) => item[key]; |
||||
|
|
||||
|
export const trackByDeep = <T = any>( |
||||
|
...keys: T extends object ? O.Paths<T> : never |
||||
|
): TrackByFunction<T> => (_, item) => keys.reduce((acc, key) => acc[key], item); |
||||
|
|
||||
@Injectable({ |
@Injectable({ |
||||
providedIn: 'root', |
providedIn: 'root', |
||||
}) |
}) |
||||
export class TrackByService<ItemType = any> { |
export class TrackByService<ItemType = any> { |
||||
by<T = ItemType>(key: keyof T): TrackByFunction<T> { |
by = trackBy; |
||||
return ({}, item) => item[key]; |
|
||||
} |
|
||||
|
|
||||
byDeep<T = ItemType>(...keys: (string | number)[]): TrackByFunction<T> { |
|
||||
return ({}, item) => keys.reduce((acc, key) => acc[key], item); |
|
||||
} |
|
||||
|
|
||||
bySelf<T = ItemType>(): TrackByFunction<T> { |
byDeep = trackByDeep; |
||||
return ({}, item) => item; |
|
||||
} |
|
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue