Question

I have a factory with a promise supposed to poll data from a webservice at an interval of 5 seconds. The data is to be fetched by a controller and parsed. The poller is initiated from app.run.

Problem is the data seems to be non-accessible from the controller, why is this?

(Just by looking at it I start to wonder if the LiveData var is threadsafe)

   factory('liveDataPoller', ['$http', '$timeout', function($http, $timeout) {
        var liveData = {
                status: -1,
                events: [],
                checksum: 0,
                serverTime: 0,
                calls: 0
            };
        var poller = function() {
                $http.get('/api/getInformation.json')
                    .then(function(res) {

                        status = res.statusCode;

                        if(status < 0) {
                            // TODO: handle service error
                        } else {
                            liveData.events = res.events;
                            liveData.checksum = res.checksum;
                            liveData.serverTime = res.serverTime;
                        }

                        liveData.status = status;

                        liveData.calls++;

                        $timeout(poller, 5000);
                    });
            };

        poller();

        return {
            getData: function() {
                return liveData;
            }
        };
    }])

The controller:

angular.module('myApp.controllers', [])
    .controller('MainCtrl', ['$rootScope', '$scope', '$timeout', 'liveDataPoller', function($rootScope, $scope, $timeout, liveDataPoller) {

           var transformLiveData = function(liveData) {
                var liveDataObj = {
                        serverTime: liveData.serverTime,
                        checksum: liveData.checksum,
                        events: [],
                        calls: liveData.calls
                    },
                    events = [],
                    i;

                if(liveData.events) {
                    for(i = 0; i < liveData.events.length; i++) {
                        events.push({
                            id:                 liveData.events[i].id,
                            name:               liveData.events[i].details[1],
                            freeText:           liveData.events[i].details[2],
                        });
                    }

                    liveDataObj.events = events;
                }

                return liveDataObj;
            }

        $rootScope.liveData = transformLiveData(liveDataPoller.getData());


    }])
Was it helpful?

Solution

The problem is that the line returning liveData in your service executed when the $http call is in progress, I would wrap the liveData object around a promise and work with that promise in the controller. Or, as a poor mans approach, you could watch the liveData object in your controller:

$scope.$watch(liveDataPoller.getData,function(value){
    console.log(value);
},true)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top