解析服务添加加载事件

Ionc parse service to add loading event

本文关键字:加载 事件 添加 服务      更新时间:2023-09-26

我创建ionic移动应用程序所有后端使用parse db,我的代码工作正常,然后我想添加ionic ($ionicLoading)事件到我的解析REST API服务,以显示加载一些东西到缓慢的互联网连接,这是我的代码-

angular.module('eventApp.services',[]).factory('eventApi',['$http','PARSE_CREDENTIALS',function($http,PARSE_CREDENTIALS){
    return {
            getAll:function()
            {
                return $http.get('https://api.parse.com/1/classes/Event',{
                    headers:{
                        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                        'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                    }
                 }
                );
            },
            get:function(id){
                return $http.get('https://api.parse.com/1/classes/Event/'+id,{
                    headers:{
                        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                        'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                    }
                });
            },
            create:function(data){
                return $http.post('https://api.parse.com/1/classes/Event',data,{
                    headers:{
                        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                        'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                        'Content-Type':'application/json'
                    }
                });
            },
            edit:function(id,data){
                return $http.put('https://api.parse.com/1/classes/Event/'+id,data,{
                    headers:{
                        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                        'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                        'Content-Type':'application/json'
                    }
                });
            },
            delete:function(id){
                return $http.delete('https://api.parse.com/1/classes/Event/'+id,{
                    headers:{
                        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                        'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                        'Content-Type':'application/json'
                    }
                });
            },
            getAllSP:function()
            {
                return $http.get('https://api.parse.com/1/classes/Speakers',{
                        headers:{
                            'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                            'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                        }
                    }
                );
            },
            getSP:function(id)
            {
                return $http.get('https://api.parse.com/1/classes/Speakers/'+id,{
                        headers:{
                            'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                            'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                        }
                    }
                );
            },
    }
}]).value('PARSE_CREDENTIALS',{
    APP_ID: 'xxx',
    REST_API_KEY:'xxx'
});

如何更改此代码以添加ionicLoading?

更好地使用拦截器,阅读这里

app.config(function($httpProvider) {
  $httpProvider.interceptors.push(function($rootScope) {
    return {
      request: function(config) {
        $rootScope.$broadcast('loading:show')
        return config
      },
      response: function(response) {
        $rootScope.$broadcast('loading:hide')
        return response
      }
    }
  })
})
app.run(function($rootScope, $ionicLoading) {
  $rootScope.$on('loading:show', function() {
    $ionicLoading.show({template: 'foo'})
  })
  $rootScope.$on('loading:hide', function() {
    $ionicLoading.hide()
  })
})

但这里仍然是肮脏的解决方案,强烈建议使用第一个

angular.module('eventApp.services',[]).factory('eventApi',['$http','PARSE_CREDENTIALS', '$ionicLoading', '$q', 'function($http,PARSE_CREDENTIALS, $ionicLoading, $q){
    return {
            getAll:function()
            {
                var defer = $q.defer();
                $ionicLoading.show({
                     template: 'Loading...'
                });
                $http.get('https://api.parse.com/1/classes/Event',{
                    headers:{
                        'X-Parse-Application-Id': PARSE_CREDENTIALS.APP_ID,
                        'X-Parse-REST-API-Key':PARSE_CREDENTIALS.REST_API_KEY,
                    }
                 }).then(function(successResponse){
                       defer.resolve(successResponse);
                    }, function(errorResponse){
                       defer.reject(errorResponse);
                 }).finally(function(){
                     $ionicLoading.hide();
                 });
                 return defer.promise;
            }
            //other methods
    }
}]).value('PARSE_CREDENTIALS',{
    APP_ID: 'xxx',
    REST_API_KEY:'xxx'
});