0

I have a question about this in AngularJs $http promises.

Here is code which demonstrates problem. Code works fine, but I will use it for my question about object this. Let say I have a function which triggers some kind of validator, which executes every 5 seconds. You can seen

$scope.startValidator = function () {
    $log.log('This start: '+this);
    DataService.validateUser(ConstService.getSessionId()).then(function (data) {
        $log.log('This then: '+this);
        $timeout(function () { 
            $log.log('This timeout: '+this);
            $scope.startValidator(); 
        }, 5000);
    });
};

And validateUser function is like this:

validateUser : function (sessionId) {
    return $http({method: 'POST', url: 'proxyServlet', responseType : 'json', data : this.createAjaxParams('/user/validate', {}, {'x-session-id' : sessionId}, 'GET', '')});
}

In code above you cann see $log.log, which returns this outputs:

This start: [object Object] angular.js:9503
XHR finished loading: "http://localhost:8080/ArrowCalcit/proxyServlet". angular.js:8081
This then: undefined angular.js:9503
This timeout: undefined 

My first question is: how can this be undefined? In first log message this is $scope, but then undefined. My second question is - can I pass this through $http (promise) so I cann access it in promise callbacks? In this case I can access $scope easily, but let say I have a service, which has 2 function - one with promise which in resolve callback should call other function in the same service.

How can I achive that? Is that even possible? getBookmarks function below is the same format as validateUser above.

angularApp
    .service('FlowService', ['DataService', 'ConstService', function (DataService, ConstService) {
        var flowService = {
            getBookmarkData : function (bookmarkId) {
                DataService.getBookmarkData(ConstService.getSessionId(), bookmarkId, this).then(
                    function (data) {                       
                    //..
                },  function (XMLHttpRequest, textStatus, errorThrown) {
                });
            },
            getBookmarks : function () {
                DataService.getBookmarks(ConstService.getSessionId, this).then(
                    function (data) {
                this.getBookmarkData(438); //this is undefiend
                },  function (XMLHttpRequest, textStatus, errorThrown) {
                });
            }
    };
    return flowService;
}]);

In this case I also get this undefined. Any solutions, suggestions for my problem? I would be thankful for any help.

Kind Regards

Sobis
  • 1,385
  • 4
  • 20
  • 29
  • why don't you just call flowService.getBookmarkData(438); instead of this.getBookmarkData? – doodeec Feb 19 '14 at 08:55
  • but if you want to have a reference to the service inside the promise callback, I think you can make a wrapper around $http inside getBookmarks method, and define the context (via call/apply) for resolving to match the service (since you are passing the service in a function parameter) - like getBookmarksPromise.resolve.call(service) – doodeec Feb 19 '14 at 09:01
  • doodeec I understand what you suggest, but I cannot find the proper way to implement this. – Sobis Feb 21 '14 at 06:30

0 Answers0