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

Breadcrumbs not displaying with resolver #24

Open
tomrainedotcom opened this issue Sep 17, 2018 · 2 comments
Open

Breadcrumbs not displaying with resolver #24

tomrainedotcom opened this issue Sep 17, 2018 · 2 comments
Assignees

Comments

@tomrainedotcom
Copy link

Can anyone help me out here? I have a custom Breadcrumb resolver to get names of events and there units from dynamic data, logging shows the array of breadcrumbs is compiled correctly but I only get a "1." displayed where the breadcrumb is supposed to be.

Is the following the correct way to return Observable<IBreadcrumb[]>?

I am using Angular 6 and have used the git repo and built out the Angular 6 converted version.

BreadcrumbResolver

import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Router, RouterStateSnapshot } from '@angular/router';
import { IBreadcrumb, McBreadcrumbsResolver } from 'ngx-breadcrumbs';
import { EventsService } from './events.service';
import { EventModel } from '../Models/event.model';
import { Observable } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class BreadcrumbResolver extends McBreadcrumbsResolver {
  private event: EventModel;
  constructor( private router: Router, private eventsService: EventsService) { super(); }

  resolve (route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<IBreadcrumb[]> {
    const breadCrumbs: Array<IBreadcrumb> = [];
    return new Observable((observer) => {
      this.eventsService.getEvent(route.params.id || route.parent.params.id).subscribe( (event) => {
        if (route.params.unitId || route.parent.params.unitId) {
          breadCrumbs.push({
            text: event.name,
            path: this.router.createUrlTree(['events', event._id]).toString()
          });
          let unitName = '';
          event.courses.forEach( (course) => {
            if (course !== null) {
              const foundUnit = course.units.find((unit) => {
                return unit.id === (route.params.unitId || route.parent.params.unitId);
              });
              if (foundUnit) {
                unitName = foundUnit.name;
                breadCrumbs.push({
                  text: unitName,
                  path: this.router.createUrlTree(['events', event._id]).toString()
                });
              }
            }
          });
          observer.next(breadCrumbs);
        } else {
          // no unit id available so we just need to event breadcrumb
          breadCrumbs.push({ text: event.name, path: this.router.createUrlTree(['events', event._id]).toString() });
          observer.next(breadCrumbs);
        }
      });
      return observer.complete();
    });
  }
}

Routing Module

const eventsRoutes: Routes = [
  {
    path: '',
    component: EventsComponent,
    data: {title: 'Events', breadcrumbs: 'Events'},
    canActivate: [AuthGuardService]
  },
  {
    path: ':id',
    component: EventComponent,
    data: {title: 'Event'},
    children: [
      {
        path: 'overview',
        component: OverviewComponent,
        data: {title: 'Event Overview', breadcrumbs: BreadcrumbResolver}
      },
      {
        path: 'participants',
        component: ParticipantsComponent,
        data: {title: 'Event Participants', breadcrumbs: BreadcrumbResolver}
      },
      {
        path: 'assessments',
        component: AssessmentsComponent,
        data: {title: 'Event Assessments', breadcrumbs: BreadcrumbResolver}
      }
    ]
  },
  {
    path: ':id/assessments/:unitId',
    component: LearningAssessmentComponent,
    data: {title: 'Learning Assessment', breadcrumbs: BreadcrumbResolver},
  },
];

Logged breadcrumbs
image

How it looks in the DOM
image

@peterbsmyth peterbsmyth self-assigned this Mar 11, 2019
@ericzon
Copy link

ericzon commented Apr 7, 2019

I have exactly the same problem... I imagine it's related how it's handled this._resolveCrumbs(route) in mc-breadcrumbs.service but I'm not very familiar with this.

@dang312
Copy link

dang312 commented May 1, 2019

Better approach here is to use resolver to retrieve data and store in route data.
Then use the route information to build breadcrums like normal ( because now in route data you have the unitName ) .
UnitResolver

@Injectable()
export class UnitResolver implements Resolve<Observable<boolean>> {
    constructor(private eventsService: EventsService) { }

    public resolve(route: ActivatedRouteSnapshot): Observable<string> {
        return new Observable<string>((observer) => {
            // Your old code , just put it back here
            this.eventsService.getEvent(route.params.id || route.parent.params.id).subscribe((event) => {
                event.courses.forEach((course) => {
                    if (course !== null) {
                        let unitName = '';
                        const foundUnit = course.units.find((unit) => {
                            return unit.id === (route.params.unitId || route.parent.params.unitId);
                        });
                        if (foundUnit) {
                            unitName = foundUnit.name;
                            observer.next(unitname);
                            observer.complete();
                        }
                        // TODO: Handle case if not found the unit here ?
                    }
                });
            });
        });
    }
}

some routing example :

{
        path: 'overview',
        component: OverviewComponent,
        data: {title: 'Event Overview', breadcrumbs: BreadcrumbResolver},
        resolve : {
           unitName: UnitResolver ,
       }
}

Usage in BreadcrumbsResolver

const unitName= routeSnapshot.data.unitName;
breadCrumbs.push({
                  text: unitName,
                  path: this.router.createUrlTree(['events', event._id]).toString()
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants