Skip to content

Instantly share code, notes, and snippets.

@kaylarose
Forked from brucecoddington/resources.js
Last active August 29, 2015 14:10
Show Gist options
  • Save kaylarose/9cc5b24b09af9cf613f7 to your computer and use it in GitHub Desktop.
Save kaylarose/9cc5b24b09af9cf613f7 to your computer and use it in GitHub Desktop.
An abstract $resource Provider for consuming RESTful resources on Angular JS
// 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;
}
});
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 !!
});
};
}
);
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');
});
@mfilotto
Copy link

Thanks for the improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment