我正在使用角度工厂在Parse.com上的课程上执行CRUD。我总共有4个类,需要在所有4上执行创建,获取,放置和删除。尽管每个URL的URL不同,但其他所有内容都保持不变。我可以将变量传递给工厂以更改api URL中的类名吗?
以下是一家工厂的示例。
.factory('Programme',['$http','PARSE_CREDENTIALS',function($http,PARSE_CREDENTIALS){
return {
getAll:function(){
return $http.get('https://api.parse.com/1/classes/Programme',{
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
}
});
},
get:function(id){
return $http.get('https://api.parse.com/1/classes/Programme/'+id,{
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
}
});
},
create:function(data){
return $http.post('https://api.parse.com/1/classes/Programme',data,{
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'
}
});
},
edit:function(id,data){
return $http.put('https://api.parse.com/1/classes/Programme/'+id,data,{
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'
}
});
},
delete:function(id){
return $http.delete('https://api.parse.com/1/classes/Programme/'+id,{
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'
}
});
}
}
}])
显然这个x4很乱。
所以我需要将网址从/Programmes
更改为/Users
/Prescription
我这样称呼我的控制器:
Programme.edit($localStorage.programme.id, {exerciseData:exercises}).success(function(data){
});
其次,我如何根据Javascript SDK将错误处理程序标记到此控制器函数?
答案 0 :(得分:5)
1)使用拦截器清理你的网址代码:将以下内容放入你的app.js
$httpProvider.interceptors.push(function ($q) {
return {
request: function(config) {
if (config.url.split('/')[0] === 'api'){
config.url = 'http://yourserverurl:3000/' + config.url.replace('api/', '');
}
return config || $q.when(config);
}
};
});
2)在你的url请求中,你现在可以通过简单的api / class调用来修改它们,你可以提供classname作为参数来重用相同的函数。 示例:
create:function(classname, data){
return $http.post('api/' + classname, data, {
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'
}
});
},
3)对于promise的错误处理,只需使用.error(errFunc) 示例:
Programme.edit($localStorage.programme.id, { exerciseData:exercises }).success(function(data){
}).error(function(response){ ... };
p.s对于1和2,请确保将$ httpProvider添加到app.js中的.config
答案 1 :(得分:2)
这是你的意思吗?
.factory('Programme',['$http','PARSE_CREDENTIALS',function($http,PARSE_CREDENTIALS){
var baseUrl = 'https://api.parse.com/1/classes/';
return {
provider:function(type) {
return {
getAll:function(){
return $http.get(getUrl(type),getParams());
},
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'
}
}
}
}
}
}])
然后你可以这样称呼它:
Programme.provider('Prescription/').edit($localStorage.programme.id, {exerciseData:exercises}).success(function(data){
}).error(function(response){ ... };
答案 2 :(得分:0)
我认为这应该是利用资源来完成的,并且还要为你的对象抽象出具体的服务,而不仅仅是把所有东西都集中到一个大工厂,这是坏习惯和形式。
为每个对象创建一个单独的服务...你也应该使用它将带来更好的代码的承诺。请记住,您阅读/复制的许多教程都是为了解释不构建生产应用程序的技术。
使用ng-resource
/**
* this factory wraps the use of angular-resource for interacting with the Kinvey
* models we have created.
* The factory also introduces the use of promises to handle the asynchronous http
* requests.
*
* @see doc - https://docs.angularjs.org/api/ngResource/service/$resource
* @see doc - $q - promises in angular - https://docs.angularjs.org/api/ng/service/$q
*/
.factory('MemberService', ['$resource', 'KINVEY', function ($resource, KINVEY) {
/**
*/
function genericErrorHandler(_error) {
console.log(_error.data);
$scope.response = _error.data;
}
var reqHeaders = {
'Authorization': KINVEY.auth
};
var Member = $resource(KINVEY.baseUrl + "members/:_id", {},
{
// headers are passed in as javascript name/value pairs
'query': {
headers: reqHeaders,
isArray: true
},
'save': {
method: 'POST',
headers: reqHeaders
},
'get': {
headers: reqHeaders
},
'update': {
method: 'PUT',
params: {_id: "@_id"},
headers: reqHeaders
},
'remove': {
method: 'DELETE',
headers: reqHeaders
}
});
return Member;
}]);
实际使用服务
/**
*
* @param $scope
*/
$scope.doList = function () {
// specific helper classes for the HTTP VERBS
MemberService.query()
.$promise.then(function (_response) {
$scope.response = _response;
}).catch(genericErrorHandler);
};
请参阅完整示例https://github.com/aaronksaunders/angular-kinvey-dataservices-example