Czy jest możliwe iw dobrej formie ponowne użycie tej samej fabryki danych w Angular?
Patrzę na generyczną fabrykę CRUD firmy Angular (której obecnie preferuję nadużywanie usługi):
app.factory('dataFactory', ['$http', function ($http) {
var urlBase = '/odata/ContentTypes';
// The _object_ defined in the factory is returned to the caller, rather than as with a service,
// where the _function_ defined in the service is returned to the caller
var dataFactory = {};
dataFactory.getContentTypes = function () {
var contentTypes = $http.get(urlBase);
return contentTypes;
};
dataFactory.getContentType = function (id) {
return $http.get(urlBase + '/' + id);
};
dataFactory.insertContentType = function (contentType) {
return $http.post(urlBase, contentType);
};
dataFactory.updateContentType = function (contentType) {
return $http.put(urlBase + '/' + contentType.ID, contentType);
}
dataFactory.deleteContentType = function (id) {
return $http.delete(urlBase + '/' + id);
}
// to traverse navigation properties
//dataFactory.getUserFromContentTypeId = function (id) {
// return $http.get(urlBase + '/' + id + '/user');
//}
//dataFactory.getOrders = function (id) {
// return $http.get(urlBase + '/' + id + '/orders');
//};
return dataFactory;
}]);
Dla wszystkich moich jednostek ten kod będzie mniej więcej taki sam. Czy możliwe jest wstrzyknięcie nazwy encji (lub odpowiadającej jej ścieżki RESTful), a jeśli tak, czy można ją traktować jak klasę częściową, gdzie jeśli wymagane są dodatkowe obietnice (takie jak właściwości nawigacyjne przejścia), można je również wprowadzić?
Jeśli jest to możliwe w Angular, czy ktoś może zamieścić kilka przykładów?