解析服务添加加载事件
Ionc parse service to add loading event
我创建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'
});
相关文章:
- 召回窗口加载事件 - javascript
- 仅加载事件序列
- cookie加载事件页面重定向
- 正在捕获动态更改其源的图像的加载事件
- 如何从vb.net中的页面加载事件中调用特定的javascript函数
- 在iframe中加载pdf时,iframe未触发加载事件
- 点击加载事件按钮
- jQuery-将加载事件附加到动态加载的元素
- 不;t使用ajax在fullcalender.js中加载事件数据
- 加载事件中的“太多递归”
- 每10秒调用一次页面加载事件
- jQuery"该参考文献“;加载事件后未设置
- ASP网格视图的重新加载事件
- 在点击和加载事件时播放暂停背景音频
- 引导模式加载事件
- 页面中的条件在卸载之前加载事件
- 如何在 asp.net 中的页面加载事件之前调用 JavaScript 确认对话框
- 位置.重新加载事件触发按钮单击事件
- iframe 加载事件中的 setTimeout 似乎没有像预期的那样等待 x 秒
- 图像加载事件未触发