代码完成PUT方法后调用GET方法

时间:2018-04-06 09:20:19

标签: typescript angular2-routing

我有一个应用程序,可以从数据库中检索用户信息。他们通过模态对话框添加用户,一旦他们点击提交,这将创建一个新用户,我试图弄清楚如何直接显示这个新用户而不必进行硬重新加载。 onSubmit()方法创建一个新用户,并且在其中我有 - this.manageUsersComponent.testGet() - 它使用get调用检索用户信息的方法。在创建用户完成之前调用此行,因此不会立即更新数据。

    public createNewUser(withLogin : boolean, validNewUser : NewUser) : Observable<any> {

        let tokenObservable : Observable<string> = withLogin ?
            this.tokenService.login() :
            new Observable(observer => {
                observer.next('');
                observer.complete();
            });

        return tokenObservable.flatMap(token => {
            if (token.length === 0) {
                token = localStorage.getItem('PlatformApiService.token');
            }

            return this.http.post<HttpResponse<any>>(URM_ENDPOINT, this.dummyNewUserRequest, {
                headers: new HttpHeaders({
                    'Authorization': 'Bearer ' + token,
                }),
                observe: 'response'});
        });
    }

    public getUsers(withLogin : boolean) : Observable<HttpResponse<any>> {
        let tokenObservable : Observable<string> = withLogin ?
            this.tokenService.login() :
            new Observable(observer => {
                observer.next('');
                observer.complete();
            });

        return tokenObservable.flatMap(token => {
            if (token.length === 0) {
                token = localStorage.getItem('PlatformApiService.token');
            }

            return this.http.get<HttpResponse<any>>(URM_ENDPOINT, {
                headers: new HttpHeaders({
                    'Authorization': 'Bearer ' + token,
                }),
                observe: 'response'});
        });
    }

    onSubmit() : void {
        this.formSubmitted = true;

        if (this.addUserForm.valid) {
            let withLogin : boolean = true;
            let unauthorized : number = 401;

            this.createValidUser();

            /**
             * Call the code to Create a new user
             */
            this.addUserService.createNewUser(!withLogin, this.validNewUser)
            .catch(error => {
                if (error.status === unauthorized) {
                    return this.addUserService.createNewUser(withLogin, this.validNewUser);
                }})
                .subscribe(
                    response => console.log(response),
                    error => console.log(error));

            this.manageUsersComponent.testGet();

            this.toggleModalDisplay();
            this.addUserForm.reset();

        } else {
            this.userValidator.validate(this.addUserForm, this.formSubmitted);
        }

    }

    public testGet() : void {
        // Fill the grid with data
        let withLogin : boolean = true;
        let unauthorized : number = 401;

        this.manageUsersService.getUsers(! withLogin)
            .catch(error => {
                if (error.status === unauthorized) {
                    return this.manageUsersService.getUsers(withLogin);
                }})
            .subscribe(
                response => this.fillTable(this.gridConfig, response.body),
                error => console.log(error));
    }

1 个答案:

答案 0 :(得分:0)

我更新了onSubmit方法,以便响应调用this.manageUsersComponent.testGet()

    onSubmit() : void {
        this.formSubmitted = true;

        if (this.addUserForm.valid) {
            let withLogin : boolean = true;
            let unauthorized : number = 401;

            this.createValidUser();

            /**
             * Call the code to Create a new user
             */
            this.addUserService.createNewUser(!withLogin, this.validNewUser)
            .catch(error => {


                if (error.status === unauthorized) {
                    return this.addUserService.createNewUser(withLogin, this.validNewUser);
                }})
                .subscribe(
                    response => this.manageUsersComponent.testGet(),
                    error => console.log(error));


            this.toggleModalDisplay();
            this.addUserForm.reset();

        } else {
            this.userValidator.validate(this.addUserForm, this.formSubmitted);
        }

    }