I have to implement a Offline and Online functionality on the click of the button.
Please help me how can I achieve this functionality.
DIY version:
First, you need to put a switch in your service. If it is under local mode, cache or store all data on client side. It'd better to have a flag in data for judging if data has been synced to server or not. If it is under server mode, do whatever it is needed to push all data to server side.
Second, you will need a monitor service in background or when server mode is enabled, syncing action will be kicked once. The action or background process will check any not synced data and push them to server. When it is done, flag it as synced.
That is the general idea.
Code of my idea to achieve this. Create a parent service class:
class BaseService {
protected execute(online: any, offline: any){
if (this.config.mode == 'online')
online();
else
offline();
}
}
In actual child service
... extends BaseService {
saveEmployee() {
this.execute(() => {
// do online stuff
}, () => {
// do offline stuff
});
}
}