Skip to content

angular-cache not working with $http #143

Description

@cboden

I'm following the documentation for using angular-cache with $http from the website and I'm getting some very unexpected behaviour:

.factory('myModel', function($http, DSCacheFactory) {
    DSCacheFactory('myModelCache'); // tried various options, didn't help

    return function() {
        return $http.get('/my/endpoint', {cache: DSCacheFactory.get('myModelCache')}).then(function(result) {
            return result.data.data;
        });
    }
})

.run(function(myModel) {
    myModel().then(function(data) {
        console.log('1: ', data);

        myModel().then(function(again) {
            console.log('3: ', again);
        });
    });

    myModel().then(function(data) {
        console.log('2: ', data);
    });
})

Only 1 and 3 are logged. Using run in this way makes for a bit of a convoluted demo, but I noticed in various places that used my model didn't have the data and this was a way to replicate the behaviour. Only one HTTP request is ever made (which is good and expected) but the data isn't always being retrieved. If I change the cache option in $http.get from DSCacheFactory to just true Angular handles the caching as expected.

I'm using Angular 1.2.23 with angular-cache 3.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions