Progress will discontinue Telerik Platform on May 10th, 2018. Learn more

Switching Between Offline and Online Mode

Switching Between Offline and Online Mode

The Backend Services JavaScript SDK maintains its current mode internally—online or offline. Regardless of it, you make the same API requests to access data—the SDK automatically forwards the request to the appropriate data store.

In online mode the SDK works with the cloud, but does some additional work to ensure that the data you request is also available offline. All other operations you invoke are also applied on the offline data.

In offline mode, the SDK uses the locally stored data. It keeps all changes you make to it so that they can be synchronized with the cloud later.

Even if you initialize the SDK with offline support enabled, the SDK still works in online mode by default. You need to implement an appropriate logic that switches the SDK online and offline, based on the requirements of your app.

Switching Unconditionally

You can explicitly switch mode any place in your code. Once you switch, the app stays in the selected mode until you switch again.

The following example shows how to switch the SDK to offline or online mode:

//Assume that you have the Everlive instance as a global variable
var el = new Everlive(...);

//Switch to online mode
el.online();

//Switch to offline mode
el.offline();

Switching Conditionally

You often need to switch the mode based on the device's connectivity state. Simply place the online() or offline() method in the appropriate event handler, like this:

//Assume that you have the Everlive instance as a global variable
var el = new Everlive(...);

//Switch to online mode when the device connects to the network
document.addEventListener("online", function() {
    el.online();
});

//Switch to offline mode when the device looses network connectivity   
document.addEventListener("offline", function() {
    el.offline();
});

You need the Network Information Cordova plugin for the above example to work in Cordova.

You have full control over what mode the app is in. Note, however, that simply switching to online does not guarantee you successful connection to Telerik Platform; there are a number of factors, such as Internet connectivity and presence of firewalls, that may prevent the device from connecting. In such situation the SDK does not switch automatically to offline mode.

Hint: you can easily simulate going online and offline in the AppBuilder simulator.

Per-Operation Settings

When using offline support you can use per-operation settings to additionally control how data operations are executed. These are discussed in the following sections.

Overriding the Global Mode

Because the current mode is set globally for the SDK instance, all data operations are forwarded to either the online or the offline data store. If you want to always run a particular data operation in either online or offline mode, you can do that by overriding the global mode when executing the operation:

el.data('MyContentType').useOffline(true).getById(...);

Preventing Applying Offline

Normally, when you enable offline support, every operation that you make online is simultaneously applied to the offline data, keeping it in sync. However, there might be cases where you do not want to apply the operation offline. The SDK provides a way to disable the offline processing for an operation. Here is an example:

el.data('MyContentType').applyOffline(false).getById(...);

See Also

Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.