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

Question: Usage with @angular/http #15

Open
michael34 opened this issue Sep 13, 2016 · 18 comments
Open

Question: Usage with @angular/http #15

michael34 opened this issue Sep 13, 2016 · 18 comments

Comments

@michael34
Copy link

michael34 commented Sep 13, 2016

Hi,

thank you for this component. How would I use it with @angular/http?
In angular 1 I use this: https://chieffancypants.github.io/angular-loading-bar/

@akserg
Copy link
Owner

akserg commented Sep 18, 2016

I plan to make small changes to support it out of the box.

@niyazhussain
Copy link

@akserg can you provide hint on how to make this working , i will fork and do . :)

@JohannesRudolph
Copy link

@akserg any news on this? let me know if we can contribute any help on this.

@michael34
Copy link
Author

michael34 commented Oct 20, 2016

I use this decorator. It's bad code but it works for now. Feel free to use/modify it for your needs.
`var WithLoadingIndicator = function (timeout = 250) {

return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
    let originalMethod = descriptor.value;
    descriptor.value = function (...args: any[]) {
        let interval: NodeJS.Timer;
        let to = setTimeout(() => {
            slimLoadingBar.start();
            interval = setInterval(() => {
                if (slimLoadingBar.progress >= 100) {
                    return;
                }
                var rnd = 0;
                //algorithm from: https://github.com/chieffancypants/angular-loading-bar/blob/master/src/loading-bar.js
                var stat = slimLoadingBar.progress;
                if (stat >= 0 && stat < 0.25) {
                    // Start out between 3 - 6% increments
                    rnd = (Math.random() * (5 - 3 + 1) + 3) / 100;
                } else if (stat >= 0.25 && stat < 0.65) {
                    // increment between 0 - 3%
                    rnd = (Math.random() * 3) / 100;
                } else if (stat >= 0.65 && stat < 0.9) {
                    // increment between 0 - 2%
                    rnd = (Math.random() * 2) / 100;
                } else if (stat >= 0.9 && stat < 0.99) {
                    // finally, increment it .5 %
                    rnd = 0.005;
                } else {
                    // after 99%, don't increment:
                    rnd = 0;
                }
                slimLoadingBar.progress = slimLoadingBar.progress + rnd * 100;
            }, 250);
        }, timeout);
        let result: Observable<any> = originalMethod.apply(this, args);               // run and store the result
        return result.map((x) => {
            clearTimeout(to);
            if (interval) {
                clearInterval(interval);
                slimLoadingBar.complete();
            }
            return x;
        }).catch((error, caught) => {
            clearTimeout(to);
            if (interval) {
                clearInterval(interval);
                slimLoadingBar.complete();
            }
            return Observable.throw(error);
        });                                               // return the result of the original method
    };
    return descriptor;
};

}
`

@shammelburg
Copy link

This will be a good implementation

@ASHFAQPATWARI
Copy link

@akserg Any milestone for this? Would be a great plugin if it can work like: automatic loading bar

@akarabach
Copy link

What if we write custom http client and start loading bar there (for every get,post,put, delete requests). Left to invent where finish loading bar.

@akarabach
Copy link

Anyone have any ideas how to do it? I can do it, but i don't know where to start :(

@aitboudad
Copy link

I already implemented, you just need to override the Http service, I'll publish it next week!

@akarabach
Copy link

Where you publish ?

@aitboudad
Copy link

see https://github.com/aitboudad/ng-loading-bar

@shammelburg
Copy link

@aitboudad Very nice, just added and it work perfectly. It'll be nice to added some properties to the component for colour and height.

Good work!!

@aitboudad
Copy link

ok can you create a new issue ?
in the meantime you can use your own css based on https://github.com/aitboudad/ng-loading-bar/blob/master/loading-bar.css

@finleysg
Copy link

finleysg commented Jan 8, 2017

Another option is to simply centralize your http calls into a single data service. I've done something like this;

private getRequest(url: string, data?: any): Observable<any> {
    let options = this.createOptions();
    let params = new URLSearchParams();
    if (data) {
        for (let key in data) {
            params.set(key, data[key]);
        }
        options.search = params;
    }
    this.loadingBar.color = 'blue';
    this.loadingBar.start();
    return this.http.get(url, options)
        .map((r: Response) => {
            this.loadingBar.color = 'green';
            this.loadingBar.complete();
            return r.json() || {};
        })
        .catch(this.handleError);
}

private postRequest(url: string, data: any) {
    this.loadingBar.color = 'blue';
    this.loadingBar.start();
    return this.http.post(url, JSON.stringify(data), this.createOptions())
        .map((response: any) => {
            this.loadingBar.color = 'green';
            this.loadingBar.complete();
            if (response._body && response._body.length > 0) {
                return response.json() || {};
            }
            return {}; // empty response
        })
        .catch(this.handleError);
}

In my central error handler, I complete the loading bar as well, but change the color to red first.

@thekalinga
Copy link

thekalinga commented Jan 25, 2017

@finleysg Problem with your implementation is, currently loading bar completes automatically if the request take a long time, lets say 2 mins.

The browser might have internal timeout, but that's browser dependent. We as developers should not be dependent on the browser settings. We should have control over this instead

Try this in the demo

@finleysg
Copy link

@thekalinga you're exactly right, but I'm willing to live with that for now. It's on my punch list to fix that, but not a priority.

@daniel-seitz
Copy link

Also when multiple simultaneous requests are made, the bar gets confused about start and finish. Interesting would be to check if the bar is already running / has open requests.

@daniel-seitz
Copy link

daniel-seitz commented Aug 26, 2017

Also the new HttpClient: @angular/common/http might provide some better options like interceptors to realize this. https://angular.io/guide/http#intercepting-all-requests-or-responses

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