I've been trying to bind the onCompleteItem array function from the ng2-file-upload package to an RxJS Observable method that I can subscribe to.
Here is function I'm talking about:
onCompleteItem(item: FileItem, response: string, status: number, headers: ParsedResponseHeaders): any;
..which in use is almost the same as:
onCompleteItem(item: any, response: any, status: any, headers: any)
Here's the relevant parts from MyComponent, which is currently working:
imageArray: Image[] = [];
uploader:FileUploader = new FileUploader({
url: '/api/FileUpload'
});
ngOnInit() {
this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
const data = JSON.parse(response);
data.forEach(x => {
console.log(x);
this.imageArray.push(x);
}
this.uploader.removeFromQueue(item);
};
}
My attempt at binding this into an Observable was through the use of the RxJS bindCallback function:
bindCallback(this.uploader.onCompleteItem, {item: any, response: any, status: any, headers: any})().pipe(
tap((item, response) => {
const data = JSON.parse(response);
this.uploader.removeFromQueue(item);
const images = data.map(x => console.log(x) );
this.imageArray.push(...images);
})
).subscribe();
This does not work, because of a few type/syntax errors. Does anyone have any good suggestions on how to get this rewritten as a bound Observable function?
bindCallbackwraps a function that's called whenever the observable is subscribed to, e.g.getJSON(url, callback). You're trying to use it just by passing thecallbackparameter.Instead you can try the
fromEventPatternfunction, which allows you to specify via its two parameters how you register the event handler, and also unregister.