Skip to content

Instantly share code, notes, and snippets.

@toddb
Created May 5, 2013 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save toddb/5522441 to your computer and use it in GitHub Desktop.
Save toddb/5522441 to your computer and use it in GitHub Desktop.
define(['services/module', 'underscore'], function (module, _) {
'use strict';
function OrderResource($http, $q, link) {
// ... other functions removed: viewState, accessState, flattenResource
// viewState is a deeper resource
// accessState looks at the Accept headers
// flattenResource: flattens all of this for the client-side resource (in-memory model bound to view)
// only GET on collection exposed (rather than get on item)
return {
get: function (links, relationshipType, mediaType) {
return link.get(links || 'HEAD', relationshipType || 'collection', mediaType).then(
function success(response) {
return $q.all(_.map(link.filter(response.data, 'item', mediaType), function (item) {
return $http.get(item.href, { headers: {accept: item.type}}).then(
function success(response) {
return $q.all([viewState(response.data, mediaType), accessState(response.headers())]).
then(flattenResource(response.data), error);
}, error)
})).
then(function (resources) {
return { items: resources, links: response.data.links};
});
},
error);
}
}
}
return module.factory('OrderResource', ['$http', '$q', 'link', OrderResource]);
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment