I am using an angular factory to perform CRUD on my classes on Parse.com. I have a total of 4 classes and need to perform create, get, put, delete on all 4. Although the URL is different for each one everything else remains the same. Can I pass variables to the factory to change the class name in the api URL?
Here is an example of one factory.
.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'
}
});
}
}
}])
Obviously this x4 is a mess.
So I need to change the URL from /Programmes to /Users /Prescription
I am calling this like from my controller like this:
Programme.edit($localStorage.programme.id, {exerciseData:exercises}).success(function(data){
});
Secondly how am I able to tag the error handler onto this controller function as per the Javascript SDK?