pkdkk
pkdkk

Reputation: 3961

AngularJS service with promise - reload/refresh

first of all, i'm a noob to AngularJS - hope someone can help with this one. I really don't get it..

I have a AngularJS app, with a "service" that return a promise, that works all well, but the problem is, if my app at the beginning is offline, and afterward gets online, now I want the service promise to refresh.

If my app is online at the beginning, it all works well. But if it is offline, and later gets online, and the reload button is clicked - the alert("TEST TEST"); never gets executed :(

.service('webService', function ($http, $q, $timeout){
    var webservice_url = "http://mywebservice_url";
    var deferred = $q.defer();

    var service = {};
    service.refresh = function() {
        $http.get(webservice_url+"?action=get_settings", {timeout: 2000})
            .success(function(data) {
                deferred.resolve(data);
            })
            .error(function(err) {
                deferred.reject({'error': err});
            });
        return deferred.promise;
    }

    this.refresh = function() {
        this.promise = service.refresh();
        return this.promise;
    }
    this.promise = service.refresh();
})



.controller('NetworkCtrl', function($scope, $location, $timeout, webService) {
    $scope.reloadClick = function(){

        webService.refresh().then(function(data) {
            alert("TEST TEST");
            console.log(data);
        });

    }
})

Upvotes: 1

Views: 1780

Answers (1)

frosty
frosty

Reputation: 21762

I am not 100% positive... cause I can't run your code... but I think that moving line three into the service.refresh method, that should get you closer.

Upvotes: 1

Related Questions