I have the following skeleton of an Angular front end reading from a backend ruby API (written in padrino).
'use strict';
(function(angular) {
function ApiAction($resource) {
return $resource('/api/',
{ },
{ api_index: {
method: "GET",
isArray: true
}
}
);
}
function whaleCtr($scope, ApiAction) {
$scope.whaleSubmit = function() {
// ApiAction.create({}, { whale_name: $scope.whaleName, age: $scope.whaleAge });
angular.forEach($scope.whales, function(d) {
ApiAction.create({}, { whale_name: d.whale_name, age: d.age });
});
};
var dolphins = ApiAction.api_index({}, {});
$scope.whales = [];
dolphins.$promise.then(function(data) {
angular.forEach(data, function(item) {
$scope.whales.push(item);
});
}, function(data) {
//if error then...
});
$scope.appendwhaleFields = function() {
var i = $scope.whales.length + 1;
$scope.whales.push({"id": i, age: "", whale_name: "" })
}
}
var whaleApp = angular.module('whaleApp', ['ngResource']);
whaleApp.controller('whaleCtr', ['$scope', 'ApiAction', whaleCtr]);
whaleApp.factory('ApiAction', ['$resource', ApiAction]);
})(angular);
I want to be able to have the ApiAction factory do a basic create one object / update one object / destroy one object in addition to being able to read all objects from the api via the '/api/' route. Is it possible to create a solution with the below structure? (obviously you can'have multiple returns as far as I know, but I want to put all the CRUD actions in the ApiAction function).
dummy code for a proposed solution (obviously you can'have multiple returns)
function ApiAction($resource) {
return $resource('/api/',
{ },
{ api_index: {
method: "GET",
isArray: true
}
}
return $resource('/api/create',
{ },
{ create: {
method: "POST",
isArray: true
}
}
);
}