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

Clear datasources on user logoff

4 Answers 68 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Insad
Top achievements
Rank 2
Insad asked on 02 Feb 2017, 10:57 AM

Hi,

I have a SPA running in which a users logs on, do all kind of things (showing grids, lists, editing data etc.) against some webservices.

When the user logs off, another user can log on.

When the new user comes on a grid/list/etc. where no data is available on the server, or an error occurs, or even while the data is loading from the server (during the 'wait-cursor-screen') the old data of the previous user is shown! Not a good thing :)

 

So at first I was hoping to build a class in which all datasources can be registered and on logoff I can just set all the data in the datasources to [].

 

Nice idea but since one cannot overload the init function of the kendo.data.DataSource we need to find another way.

 

On each fetch() might be an idea but really unwanted because it means that all fetch functions must be overloaded and for each fetch we must check if the datasource is already registered etc.etc. Takes too much time, so again we need another way..

At this moment I'm a bit of ideas and am under the impression I must be missing something really obvious..

 

Any ideas? Anybody?

 

Best regards,

 

Insad

 

4 Answers, 1 is accepted

Sort by
0
Insad
Top achievements
Rank 2
answered on 02 Feb 2017, 11:17 AM

Okay,

I found the way to overload the init of the DataSource by extending the kendo.data.DataSource into my own class.

But the question remains, isn't there a simpler way (something like the good old flushall() from the good old C programming language ;))

For now I will try to implement my idea, any suggestions are welcome.

 

Insad

0
Insad
Top achievements
Rank 2
answered on 02 Feb 2017, 01:30 PM

For everyone who would like to know how I fixed the problem:

//...
// Insad class definition -- By Albert van Peppen
var classInsad = kendo.Class.extend({
//...
//#region -- Overloads on existing Kendo classes --
    , arrDataSources: []
    , DropArrayDataSources: function ()
    {
        this.arrDataSources.forEach(function(entry) {
            if (entry._data !== undefined && entry._data !== null && entry._data.length > 0) {
                console.log(' - Has data: ', entry._data.length);
                //entry._data.splice(0, entry.data.length);     // This doesn't remove the data!
                // Use slow way; that does work
                while(entry._data.length > 0) {
                    entry._data.pop();
                }
            }
        });
    }
    , AddArrayDataSources: function (dataSource)
    {
        this.arrDataSources.push(dataSource);
    }
 
    // Extend Kendo DataSource
    , DataSource: kendo.data.DataSource.extend({
        init: function(options) {
            //console.log('Constructor of Insad.DataSource', this);
            Insad.AddArrayDataSources(this);
            kendo.data.DataSource.fn.init.call(this, $.extend({}, this.defaults, options));
        }
    })
//#endregion
});
//...
var Insad = new classInsad();
//...
// From now on use Insad.DataSource instead of kendo.data.DataSource

Perhaps there is a better way, but for now this works :)

 

Albert van Peppen (Insad)

 

0
Insad
Top achievements
Rank 2
answered on 02 Feb 2017, 01:34 PM

PS. After logging off you should call

Insad.DropArrayDataSources();

But I guess you figured that out yourselves ;)

 

0
Accepted
Stefan
Telerik team
answered on 06 Feb 2017, 07:49 AM
Hello Albert,

Thank you for sharing this solution with the Kendo UI community.

Usually, the dataSource can be cleared by passing empty data, to its data method, and this could be done once the user logs off:

http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#methods-data

Still, this solution may not work on your specific scenario, and I'm happy to hear that you have a working approach already.

Regards,
Stefan
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Data Source
Asked by
Insad
Top achievements
Rank 2
Answers by
Insad
Top achievements
Rank 2
Stefan
Telerik team
Share this question
or