Clear datasources on user logoff

5 posts, 1 answers
  1. Insad
    Insad avatar
    28 posts
    Member since:
    Jan 2010

    Posted 02 Feb Link to this post

    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

     

  2. Insad
    Insad avatar
    28 posts
    Member since:
    Jan 2010

    Posted 02 Feb Link to this post

    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

  3. Insad
    Insad avatar
    28 posts
    Member since:
    Jan 2010

    Posted 02 Feb in reply to Insad Link to this post

    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)

     

  4. Insad
    Insad avatar
    28 posts
    Member since:
    Jan 2010

    Posted 02 Feb in reply to Insad Link to this post

    PS. After logging off you should call

    Insad.DropArrayDataSources();

    But I guess you figured that out yourselves ;)

     

  5. Answer
    Stefan
    Admin
    Stefan avatar
    421 posts

    Posted 06 Feb Link to this post

    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.
Back to Top