StateRestore calls save for every state upon load, is that a bug or a feature?

StateRestore calls save for every state upon load, is that a bug or a feature?

desperadodesperado Posts: 159Questions: 33Answers: 4
edited December 2021 in Extensions

I have implemented the ajax function method of loading and save/rename/update.

When I load a page that has a DataTable with 10 saved states I get a call to load as expected and then I get 10 calls to save upon calling the callback to load the data into stateRestore.

I don't see how this could be a "feature" so I suspect it is a bug. I am able to fix by setting a boolean to true prior to my call to callback(cbData) in load and then checking for that boolean on save and ignoring the request. But I thought I would ask this questions for 2 reasons. 1) Perhaps there is a good reason for this and I should NOT ignore the calls, 2) If it is a bug it can be addressed.

In case anyone else wants to avoid this load here is how I implemented my solution

let _stateRestoreBlockSaveOnLoad = false;
...
if (data.action === 'load') {
            let url = _rcoiBaseUrl + "dataTablesView/getViews/" + _dataTableViewStoreDataTableId;
            $.get(url,function (data,status) {
                let cbData = {};
                for (let i = 0; i < data.length; i++) {
                    // Convert back from the string created when saving
                    cbData[data[i].identifier] = JSON.parse(data[i].state);
                }
                // Load states into StateRestore
                console.log("starting callback");
                // Set Block on Save to true so saves can be skipped when loading data
                _stateRestoreBlockSaveOnLoad = true;
                callback(cbData);
                // Unset the Save Block since loading is complete
                _stateRestoreBlockSaveOnLoad = false;
                console.log("Finished callback")
            });
        } else if (data.action === 'save') {
            // Check if we are loading initial states and ignore calls to save
            if (_stateRestoreBlockSaveOnLoad) {
                // Fix issue with stateRestore calling save for every State when loading
                console.log("Blocked Save on Load from State Restore")
                return;
            }
            let ids = Object.keys(data.stateRestore);
            for (let i = 0; i < ids.length; i++) {
                // Add all of the states listed in the object
                let url = _rcoiBaseUrl + "dataTablesView/saveView";
                let viewId=ids[i];
                let viewData=JSON.stringify(data.stateRestore[ids[i]]);
                let _viewSaveRequestData = {
                    'viewId': viewId,
                    'viewData': viewData,
                    'dataTableId': _dataTableViewStoreDataTableId
                };
                // This allows the post to pass the security token verification
                // without this data you will get a 401 error
                _viewSaveRequestData[_csrf_param_name] = _csrf_token; // Adds the token
                console.log("Saving");
                $.post(url,_viewSaveRequestData,ajaxSaveCompleted);
            }
...

This question has an accepted answers - jump to answer

Answers

  • sandysandy Posts: 913Questions: 0Answers: 236
    Answer ✓

    Hi @desperado ,

    Yes this is meant to happen. Consider predefined states - it is possible to load these from the config or over ajax, but this does not guarantee that they have been saved in the ajax source of the local storage so a save has to occur at that point. It is also possible that people may wish to load predefined states over ajax and then save them to local storage.

    How your ajax function behaves is totally down to you, so it is valid for you do set a flag as you have above and then skip the saving.

    Thanks,
    Sandy

Sign In or Register to comment.