user1955726
user1955726

Reputation: 23

angular factory return data remained same after changing the json file

i am calling a camera.json file from "cameraData" service and injecting the cameradata service in to "CameraController". If i change the camera.json after clicked on refresh button i am getting old data. Any idea?

.factory('cameraData', function ($http, $q,globalVariable) { var deferred = $q.defer();

var cameraData = {};     
var contentType  = "application/json; charset=utf-8";   

cameraData.GetItemList = function(){ 
    $('.loader').show();
    var senddata ={};
    senddata.installedcameraid = "9547857793457943";
      $http({
        //url: globalVariable.ServerAddress + "Admin_GetCameraPoints",
        url: globalVariable.Camerafilepath,
        dataType: 'json',
        method: "POST",
        data: JSON.stringify(senddata),
        headers: {
            "Content-Type": contentType,
            "access_token": globalVariable.TOKEN
        }

    }).success(function(response){ 
        //$scope.response = response;
        deferred.resolve(response);
        return deferred.promise;

    }).error(function(error){
        //$scope.error = error;
        deferred.reject(error);
    });


    return deferred.promise;
}


return cameraData;

})

.controller('CameraController',function($scope,$timeout,cameraData){ $scope.refreshCameraData = function(){

    $scope.allCamera = [];
    cameraData.GetItemList()
    .then(function(data) {
        $scope.allCamera = data.Camera;
    }, function(err) {
        // promise rejected, could log the error 
        console.log('error', err);
    });

}

cameraData.GetItemList()
    .then(function(data) {
        $scope.allCamera = data.Camera;
    }, function(err) {
        // promise rejected, could log the error 
        console.log('error', err);
    });

})

Upvotes: 0

Views: 117

Answers (2)

Aditya Singh
Aditya Singh

Reputation: 16660

You don't need to explicitly create a deferred object and resolve/ reject it manually. the $http() method itself returns a promise and can be directly returned as below.

.factory('cameraData', function ($http, $q,globalVariable) {
    var cameraData = {};
    $('.loader').show();        
    var contentType  = "application/json; charset=utf-8"; 

    cameraData.GetItemList = function(){
        $('.loader').show();
        var senddata ={};
        senddata.installedcameraid = "9547857793457943";

        return $http({
            //url: globalVariable.ServerAddress + "Admin_GetCameraPoints",
            url: globalVariable.Camerafilepath,
            dataType: 'json',
            method: "POST",
            data: JSON.stringify(senddata),
            headers: {
                "Content-Type": contentType,
                "access_token": globalVariable.TOKEN
            }

        }).then(function(response){ 
            $('.loader').hide();
        }).error(function(error){
            $('.loader').hide();
        });
    }

   return cameraData;
});

Also, for hiding the loader you can use the .finally() method as below:

        return $http({
            url: globalVariable.Camerafilepath,
            dataType: 'json',
            method: "POST",
            data: JSON.stringify(senddata),
            headers: {
                "Content-Type": contentType,
                "access_token": globalVariable.TOKEN
            }

        }).finally(function(response){ 
            $('.loader').hide();
        });

In your controller, you can now access the response as,

.controller('CameraController',function($scope,$timeout,cameraData){

    $scope.refreshCameraData = function(){

        $scope.allCamera = [];

        cameraData
            .GetItemList()
            .then(function(response) {
                $scope.allCamera = response.data.Camera;
            }, function(err) {
                // promise rejected, could log the error 
                console.log('error', err);
            });

    }


    cameraData.GetItemList()
        .then(function(response) {
            $scope.allCamera = response.data.Camera;
        }, function(err) {
            // promise rejected, could log the error 
            console.log('error', err);
        });
    })
})

Upvotes: 1

dhavalcengg
dhavalcengg

Reputation: 4713

You need to create defer object everytime you call factory method. It should return new promise everytime you call api. Change your factory code with following.

var cameraData = {};
$('.loader').show();        
var contentType  = "application/json; charset=utf-8";   

cameraData.GetItemList = function(){
    // need to create defer object everytime 
    var deferred = $q.defer(); 
    $('.loader').show();
    var senddata ={};
    senddata.installedcameraid = "9547857793457943";
      $http({
        //url: globalVariable.ServerAddress + "Admin_GetCameraPoints",
        url: globalVariable.Camerafilepath,
        dataType: 'json',
        method: "POST",
        data: JSON.stringify(senddata),
        headers: {
            "Content-Type": contentType,
            "access_token": globalVariable.TOKEN
        }

    }).success(function(response){ 
        //$scope.response = response;
        $('.loader').hide();
        deferred.resolve(response);
        return deferred.promise;

    }).error(function(error){
        //$scope.error = error;
        $('.loader').hide();
        deferred.reject(error);
    });


    return deferred.promise;
}


return cameraData;
})

Upvotes: 0

Related Questions