This is a migrated thread and some comments may be shown as answers.

Best practices for Expiring/refreshing dataSource data

3 Answers 138 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Marlon
Top achievements
Rank 1
Marlon asked on 23 Jul 2013, 06:22 PM
I'm working on a mobile app were some of the data requests will be cached in localStorage on the device. Depending on the data, I'd like to assign a timeout value for it that would flush the existing value and make new network request. I'm assuming I'll need to set timestamps into localStorage, check them when the data is requested and if its expired, execute a read() on the object to refresh the data.

Would that be a plausible solution or are there features of the framework that I've overlooked for this type of thing?

3 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 25 Jul 2013, 08:06 AM
Hello Marlon,

Basically what the dataSource provides you when you want to refresh the records with fresh copies from the server is to use the dataSource.read() method.

Please notice that refreshing just a single record from the server is not possible - when read is performed all the records for the given page/filter/sort descriptors will be fetched from the server.

Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Marlon
Top achievements
Rank 1
answered on 25 Jul 2013, 06:37 PM
Hi Petur,

Thank you for the response.  I may not have phrased my question correctly...the issue that I'm having is that I'm building a mobile app where some of the data needs to persist...in case the app is closed completely and then opened while the device is offline, we want to keep some pieces of data. 

In the prototype I'm working on, I've wrapped the dataSource in a deferred object that checks against the localStorage, if its expired, .read() is called.

I feel like there's an easier way to accomplish this but I just wanted to know if  there is a better solution. I like the binding functionality wrapped in dataSource but I need an extra layer to manage how/when new data is requested.
var listNotifications = function(){
        var deferred = $.Deferred();
 
        var bool = utils.dataStoreIsValid(constants.listNotifications);
        if(bool) {
            var payload = utils.getStorage(constants.listNotifications);
                   notificationsDataSource.data(payload)
                   deferred.resolve(payload);
        } else {
            notificationsDataSource.bind('change', function(){
                deferred.resolve(notificationsDataSource.data());
            });
            notificationsDataSource.read();
        }
        return deferred.promise();
    }
0
Petur Subev
Telerik team
answered on 29 Jul 2013, 01:44 PM
Hello Marion,

I am sorry I misunderstood the case.

Generally speaking the idea that you shared sounds just fine - if there is no items in the localStorage use the dataSource.read to fetch the records from the server, otherwise load the items from the localStorage with the dataSource.data method.

Kind Regards,
Petur Subev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Data Source
Asked by
Marlon
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
Marlon
Top achievements
Rank 1
Share this question
or