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?