0

Angular 2 - RC.5

Router - 3.0.0-rc.1

I feel like I'm really close to the solution for this but I've run out of ideas.

My problem is very similar to this StackOverflow question but I'm not able to make it work.

I have a canActivate method in a guard that needs to retrieve an Observable form that has an array of admins in it to then compare that list against the logged in user. It looks like this without all the admin checking logic:

canActivate(next: ActivatedRouteSnapshot,state: RouterStateSnapshot):Observable<boolean>|boolean {
    console.log('In canActivate')
    return this.formService.currentForm$.map((form) => {
      console.log("In Observable")
        if (form.admins.length > 0) {
          console.log("GOOD!!")
          return true
        }
        console.log("BAD!!!")
        return false;
      }).take(1)

I'm suspicious that the currentForm$ observable is setup wrong but I don't know how. Here is the related information in the FormService.

@Injectable()
export class FormService {
  private currentFormSource: Subject<Form>;

  constructor() {
    this.currentFormSource = new Subject<Form>();
  }

  get currentForm$() {
    return this.currentFormSource.asObservable();
  }
}

I currently get no errors, it just doesn't do anything. Nothing is getting logged to the console except for In canActivate and the page is still restricted. Thanks for any advice you can give.

UPDATE

Inside the FormService is a method called getForm('form-name') that's supposed to get called when the form.component file gets initialized. This emits this.currentFormSource.next(this.dataStore.currentForm);. I see that would be a problem because it's emitting before it's subscribed to by the canActivate call. But I also just discovered the getForm method is not getting called at all because the canActivate child is being checked before the parent component gets loaded so I've got to find some other place to set the form. I could do it in the guard but I was hoping to use it as a general guard for multiple different forms that would be setting the currentForm from their parent components.

It's like this.

/form(1,2,3,4,5, etc)
/form(1,2,3,4,5,etc)/admin

I want to set the current form from the parent form component but it doesn't get loaded if canActivate of the admin child route doesn't pass. If there was a way to pass a variable into the routes 'canActivate' I could work with that. Suggestions?

Community
  • 1
  • 1
andyrue
  • 903
  • 10
  • 24
  • The `currentForms$` observable doesn't seem to emit an event anywhere. You also don't need to use an observable. Simply returning `true` and `false` is supported as well. – Günter Zöchbauer Aug 25 '16 at 04:27
  • @GünterZöchbauer I've updated the question. I see I'm facing different challenges than I originally thought. – andyrue Aug 25 '16 at 13:23
  • Change it to `private currentFormSource: BehaviorSubject
    ;` then the last value gets emitted immediately again for each subscriber.
    – Günter Zöchbauer Aug 25 '16 at 13:39
  • @GünterZöchbauer That gets a response but still leaves me with the challenge of where to set the current form. Right now I'm only able to get a default value. I need to set the form before canActivate is called but there's not a good place to do it since the parent component isn't getting initialized. I would have to set it at the app.component but at that point it doesn't (and shouldn't) know which form to set. I might just need to make custom guards for each form type. – andyrue Aug 25 '16 at 14:03

0 Answers0