question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Catching loading event

See original GitHub issue

Hello, I want to make a spinner indicating data being refreshed. But how can I catch event for data being loaded?

Im getting data this way:

this.source = new ServerDataSource(http, { endPoint: 'https://jsonplaceholder.typicode.com/photos' });

I tried to user LocalDataSource, and manually getting data on “onSearch”:

getData(modifiedData?) {
        this.loading = true;
        let sourceCopy = modifiedData ? modifiedData : this.source;
        let requestData = {
            filter: sourceCopy.getFilter(),
            paging: sourceCopy.getPaging(),
            sort: sourceCopy.getSort()
        }
        this.service.getData(requestData).then((data) => {
            this.source.load(data);
            this.loading = false;
        });
    }

This works for search input, but this way I’d have to write pagination component on my own, while the stock one is just fine.

Any ideas?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:9

github_iconTop GitHub Comments

1reaction
Squidy06commented, Jul 2, 2018

@MrPardeep Yup Take and import this: https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/server/serve.data-source.ts

In component:

constructor(http: Http) {
    this.http = http;
    this.source = new CustomServerDataSource(this.http, {
        endPoint: 'https://jsonplaceholder.typicode.com/posts?',
    });
    this.source.onUpdateStarted().subscribe(e => {
        setTimeout(() => {
            this.loading = true;
        });
    });
}
ngOnInit(): void {
    this.source.onChanged().subscribe(e => {
        setTimeout(() => {
            this.loading = false;
        });
    });
}

In your new data-source.ts

protected onUpdateStartedSource = new Subject<any>();
protected emitOnUpdateStarted(element: any) {
    this.onUpdateStartedSource.next(element);
}
onUpdateStarted(): Observable<any> {
    return this.onUpdateStartedSource.asObservable();
}

And add to your getElements() function in data-source.ts

this.emitOnUpdateStarted();
0reactions
imbrodcommented, Jan 21, 2022

@MrPardeep Yup Take and import this: https://github.com/akveo/ng2-smart-table/blob/master/src/app/pages/examples/server/serve.data-source.ts

This link is broken. Does anybody have steps to create custom class from ServerDataSource? I managed to write something like this, but I guess there could be more elegant way:

export class MyDataSource extends ServerDataSource {
    constructor(http: HttpClient, conf?: ServerSourceConf | {}) {        
        super(http, conf);
        this.init();
      }     
      public init(): Promise<any> {
          let endPoint = `${environment.apiEndpoint}/${this.conf.endPoint}`;
          this.conf.endPoint = endPoint;
          this.conf.sortFieldKey = 'orderBy';
          this.conf.sortDirKey = 'sortDirection';
          this.conf.pagerPageKey = 'pageNumber';
          this.conf.pagerLimitKey = 'pageSize';
          this.conf.filterFieldKey = null;
          this.conf.totalKey = 'count';
          this.conf.dataKey = 'value';
          return super.getElements();
      }
}

call:

this.source = new MyDataSource(this.http, {
	endPoint: 'myMethod'
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Window: load event - Web APIs - MDN Web Docs - Mozilla
The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets, scripts, iframes, and images.
Read more >
JavaScript how to catch event when lazy loaded script is ready?
I have a lazy loading JavaScript file, how can I catch the event when the class in the file is ready for usage?...
Read more >
Help with "catching" an event AFTER the grid is fully loaded ...
Hello. I'm having a problem implementing the following scenario: After the grid is loaded (binding to local data), in some cases, ...
Read more >
Handling Events :: Eloquent JavaScript
When a page finishes loading, the "load" event fires on the window and the document body objects. This is often used to schedule...
Read more >
Resource loading: onload and onerror
There are two events for it: onload – successful load,; onerror – an error occurred. Loading a script. Let's say we need to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found