1

Throughout my project I have a piece of code that performs CRUD with Parse.com via the REST API. I would like to insert a function in the error handler that is a universal (project wide) error function.

I have considered a factory / service or possibly passing something via $rootScope.

Secondly I would like to wrap all my api calls in a timeout function so that if there is no response in a certain time then I can notify the user with a useful notification.

ParseFactory.provider('Programme').getAll().success(function(data){
     $scope.programmes = data.results;

  }).error(function(response){
    $ionicPopup.alert({
    title: 'Unable to load - Please check your connection '
    });

  });

Any examples of how to best achieve this would be greatly received.

=== EDIT - adding ParseFactory Code ===

.factory('ParseFactory',['$http','PARSE_CREDENTIALS',function($http,PARSE_CREDENTIALS){
    var baseUrl = 'https://api.parse.com/1/classes/';
        return {
            provider:function(type) {
                var userQuery = {'userId': Parse.User.current().id};
                return {
                    getAll:function(){
                        return $http.get(getUrl(type),getUserParams());
                    },
                    get:function(id){
                        return $http.get(getUrl(type)+id,getParams());
                    },
                    create:function(data){
                        return $http.post(getUrl(type),data,getParams());
                    },
                    edit:function(id,data){
                        return $http.put(getUrl(type)+id,data,getParams());
                    },
                    delete:function(id){
                        return $http.delete(getUrl(type)+id,getParams());
                    }
                }
                function getUrl(type) {
                    return baseUrl+type;
                }

                function getParams() {
                    return {
                            headers:{
                                'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                                'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                                'X-Parse-Session-Token': PARSE_CREDENTIALS.PARSE_SESSION,
                                'Content-Type':'application/json'
                            }      
                    }    
                }
            }
        }

    }])

Many thanks

1 Answer 1

1

Regarding single error handler function which shows the ionicPopup:

You can emit an event using $rootScope.$emit from the error callback functions. You can listen to all those events using $rootScope.$on in either run() method or in a factory and it will show the $ionicPopup

In your error handler:

$rootScope.$emit('errorEvent',
           {"message" : "Pass your custom message here", "errorObject": errorObject} 
);

In your event listener

app.run(function($ionicPopup) {
    $rootScope.$on("errorEvent", function(event, data) {
      // you can access that here
       $ionicPopup.alert({
            title: data.message
        });
   });
});

Update:

For the timeout option, in your getParams() function add timeout config property

 function getParams() {
     return {
         timeout: 18000, // adjust this value
         headers: {
             'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
             'X-Parse-REST-API-Key': PARSE_CREDENTIALS.REST_API_KEY,
             'X-Parse-Session-Token': PARSE_CREDENTIALS.PARSE_SESSION,
             'Content-Type': 'application/json'
         }
     }
 }
Sign up to request clarification or add additional context in comments.

7 Comments

Ok that's kinda cool. Can I pass a variable depending on what I want the popup to say? Or include the returned error from Parse?
Yes, you can. Will add a sample
@Taylorsuk added an example for you :)
Do you have any ideas on timeout functions?
Share parsefactory code. In case you are using $http then you can set the timeout property in that with the time limit
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.