After I decided that $resource was not my thing, I wanted to write my own factory to play with my data. Having half written this first one, I am now trying to figure out the best way to reuse this code or my different restful objects. Should I wrap up the code below in a service where I define url and key and then create individual factories that use the service? Or do I put a factory inside a factory? I guess my end result is that I want to be able to create my factories where I define 'object' and 'key'.
app.factory('AssignmentData', function($http, apiUrl){
var object = 'assignments'
var key = 'assignmentid';
var url = apiUrl + object + '/';
var actions = {
query : function(params){
return $http({
method: 'GET',
url: url,
params: params
}).then(function(response){return response.data},
function(error){return error})
},
get : function(params){
return $http({
method: 'GET',
url: url + params[key],
params: _.omit(params, key)
}).then(function(response){return response.data},
function(error){return error})
},
save : function(object){
return true;
},
delete : function(object){
return true;
}
}
return actions;
});
I don't think I asked my question correctly. It probably had nothing to do with angularjs and was just my not knowing how to write javascript. In the end, i wrote my factory as follows which accomplished my goal (or at least I think it did)
schedApp.factory('RestData', function($http, apiUrl){
var resource = function(object){
var url = apiUrl + object +'s';
var key = object + 'id';
var id = 'id';
var actions = {
query : function(params){
return $http({
method: 'GET',
url: url,
params: params
}).then(function(response){return response.data},
function(error){return error})
},
get : function(params){
return $http({
method: 'GET',
url: url + '/' params[key],
params: _.omit(params, key)
}).then(function(response){return response.data},
function(error){return error})
},
save : function(object){
return true;
},
delete : function(object){
return true;
}
}
return actions;
}
return resource;
});