Skip to content

Commit

Permalink
feat(ViewLocator): enable custom view selector functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EisenbergEffect committed Aug 22, 2019
1 parent 135f4a6 commit ee6f03f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions packages/runtime/src/resources/value-converters/view.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { ComposableObject, IViewLocator } from '../../templating/view';
import { ComposableObject, IViewLocator, ViewSelector } from '../../templating/view';
import { valueConverter } from '../value-converter';

@valueConverter('view')
export class ViewValueConverter {
constructor(@IViewLocator private readonly viewLocator: IViewLocator) {}

public toView(object: ComposableObject | null | undefined, viewName?: string) {
public toView(object: ComposableObject | null | undefined, viewNameOrSelector?: string | ViewSelector) {
return this.viewLocator.getViewComponentForObject(
object,
viewName
viewNameOrSelector
);
}
}
15 changes: 10 additions & 5 deletions packages/runtime/src/templating/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,11 @@ export const IViewLocator = DI.createInterface<IViewLocator>('IViewLocator')
.withDefault(x => x.singleton(ViewLocator));

export interface IViewLocator {
getViewComponentForObject(object: ComposableObject | null | undefined, requestedViewName?: string): Constructable | null;
getViewComponentForObject(object: ComposableObject | null | undefined, viewNameOrSelector?: string | ViewSelector): Constructable | null;
}

export type ComposableObject = Omit<IViewModel, '$controller'>;
export type ViewSelector = (object: ComposableObject, views: ITemplateDefinition[]) => string;

const lifecycleCallbacks = [
'binding',
Expand All @@ -140,10 +141,14 @@ export class ViewLocator implements IViewLocator {
private modelInstanceToBoundComponent: WeakMap<object, Record<string, Constructable>> = new WeakMap();
private modelTypeToUnboundComponent: Map<object, Record<string, Constructable>> = new Map();

public getViewComponentForObject(object: ComposableObject | null | undefined, viewName?: string) {
if (object && hasAssociatedViews(object.constructor)) {
const availableViews = object.constructor.$views;
const resolvedViewName = this.getViewName(availableViews, viewName);
public getViewComponentForObject(object: ComposableObject | null | undefined, viewNameOrSelector?: string | ViewSelector) {
if (object) {
const availableViews = hasAssociatedViews(object.constructor)
? object.constructor.$views
: [];
const resolvedViewName = typeof viewNameOrSelector === 'function'
? viewNameOrSelector(object, availableViews)
: this.getViewName(availableViews, viewNameOrSelector);

return this.getOrCreateBoundComponent(
object,
Expand Down

0 comments on commit ee6f03f

Please sign in to comment.