Angular related to invalid cached data

Well, this may be a long post, but please do not click, you may know a simple answer.

Happening:

Suppose you have created an angular application in which people log in, perform some operations, and then log out again. The application will collect data from the API using the factory and the service, and in order to make the download applicationeven faster, you will save this data in such variables:

app.factory("divisionService", function (api, $http, $q) {
var division = {};
var divisionArray = [];
var mergedUserList = [];
return {
    getList: function () {
        var d = $q.defer();
        if (divisionArray <= 0) {
            $http.get(api.getUrl('divisionWithUsers', null))
                .success(function (response) {
                    divisionArray = response;
                    d.resolve(divisionArray);
                });

        }
        if (divisionArray.length > 0) {
            d.resolve(divisionArray);
        }
        return d.promise;
    },

This ensures that if a user tries to use a controller that uses divisionService, then that user will instantly receive data if it is already selected.

Problem:

, ( /) . factory, , , , , , !

angular singletons, , .

: ", ", , .

, : ? , angular ?

+4
3

clear()

clear() divisionService factory, (, ,...)

app.factory("divisionService", function () {
    var division = {};
    var divisionArray = [];
    var mergedUserList = [];
    return {
        clear: function(){
            // Clear the cached data

            for (var key in division)
            {
                delete division[key];
            }

            divisionArray.length = 0;

            // ...
        },
        getList: ...
    }
});

function logout(){
    divisionService.clear();
}

, , (, divisionService.clear())

function logout(){
    $window.location.reload();
}

, ()

+3

 divArray = [];

. , . , .

+1

You can also cache user information and compare it to see if the user has changed before deciding to update the data.

+1
source

All Articles