-
-
Save kaylarose/9cc5b24b09af9cf613f7 to your computer and use it in GitHub Desktop.
An abstract $resource Provider for consuming RESTful resources on Angular JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original Source: | |
// - http://www.objectpartners.com/2014/06/03/extending-angulars-resource-service-for-a-consistent-api/ | |
// - https://gist.github.com/brucecoddington/92a8d4b92478573d0f42 | |
// | |
// Enhancements/Fixes: | |
// - Delete by Query instead of Model Instance | |
// - Delete single by Model | |
// - Added ability to have Root Host appended to all resources registered. | |
// - Added Count action. (TODO Configurable) | |
// - Fixed lots of Misc. Dependency Injection/Recursion, scope issues. | |
angular.module('app.resources', ['ngResource']) | |
.factory('api', function ($resource) { | |
var api = { | |
defaultConfig : {id: '@id'}, | |
extraMethods: { | |
'update' : { | |
method: 'PUT' | |
} | |
}, | |
add : function (config) { | |
var params, | |
url; | |
// If the add() function is called with a | |
// String, create the default configuration. | |
if (angular.isString(config)) { | |
var configObj = { | |
resource: config, | |
url: '/' + config | |
}; | |
config = configObj; | |
} | |
// If the url follows the expected pattern, we can set cool defaults | |
if (!config.unnatural) { | |
var orig = angular.copy(api.defaultConfig); | |
params = angular.extend(orig, config.params); | |
url = config.url + '/:id'; | |
// otherwise we have to declare the entire configuration. | |
} else { | |
params = config.params; | |
url = config.url; | |
} | |
// If we supply a method configuration, use that instead of the default extra. | |
var methods = config.methods || api.extraMethods; | |
api[config.resource] = $resource(url, params, methods); | |
} | |
}; | |
return api; | |
}) | |
.provider('data', { | |
list : function (resource, query) { | |
return [ | |
'data', | |
function (data) { // inject the data service | |
return data.list(resource, query); | |
} | |
] | |
}, | |
get: function (resource, query) { | |
return [ | |
'data', | |
function(data) { | |
return data.get(resource, query); | |
} | |
] | |
}, | |
$get: function (api) { | |
var data = { | |
list: function (resource, query) { | |
return api[resource].query(query).$promise; | |
}, | |
get : function (resource, query) { | |
return api[resource].get(query).$promise; | |
}, | |
create : function (resource, model) { | |
return api[resource].save(model).$promise; | |
}, | |
update : function (resource, model) { | |
return api[resource].update(model).$promise; | |
}, | |
remove : function (resource, query) { | |
return api[resource].remove(query).$promise; | |
}, | |
removeOne : function (resource, model) { | |
return api[resource].remove({id: model._id}).$promise; | |
} | |
}; | |
return data; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('app.timesheets.controllers', [ | |
'timesheet.directives' | |
]) | |
.controller('TimesheetCtrl', function (data, $scope, $state, $stateParams, timesheets) { | |
$scope.requestTimesheets = function requestTimesheets (page) { | |
var query = { | |
user_id: $stateParams.user_id | |
}; | |
data.list('timesheets', query) | |
.then(function (pageConfig) { | |
$scope.timesheets = timesheets; | |
}); | |
}; | |
$scope.remove = function remove (timesheet) { | |
data.remove('timesheets', timesheet) | |
.then(function () { | |
// success !! | |
}) | |
.catch(function (x) { | |
// error !! | |
}); | |
}; | |
} | |
) | |
.controller('TimesheetDetailCtrl', function ($scope, $state, $stateParams, data, timesheet) { | |
$scope.timesheet = timesheet; | |
} | |
) | |
.controller('TimesheetEditCtrl', function ($scope, $state, $stateParams, data, timesheet) { | |
$scope.timesheet = timesheet; | |
$scope.save = function save () { | |
$scope.timesheet.$update() | |
.then(function (updated) { | |
$scope.timesheet = updated; | |
// success !! | |
}) | |
.catch(function (x) { | |
// error !! | |
}); | |
}; | |
} | |
); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('app.timesheets', [ | |
'app.timesheets.controllers', | |
'ui.router', | |
'authorization.services' | |
]) | |
.config(function ($stateProvider, dataProvider) { | |
$stateProvider | |
.state('app.timesheets', { | |
url: '/timesheets', | |
controller: 'TimesheetCtrl', | |
templateUrl: 'assets/templates/app/timesheets/index.html' | |
}) | |
.state('app.timesheets.detail', { | |
url: '/:id', | |
controller: 'TimesheetDetailCtrl', | |
templateUrl: 'assets/templates/app/timesheets/detail.html', | |
resolve : { | |
timesheet : ['$stateParams', 'data' function ($stateParams, data) { | |
return data.get('timesheets', $stateParams); | |
}] | |
} | |
}); | |
}) | |
.run(function (api) { | |
api.add('timesheets'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the improvement.