(config)
| 811 | this.$get = ['$http', '$q', function($http, $q) { |
| 812 | |
| 813 | function createServiceForConfiguration(config) { |
| 814 | var service = {}; |
| 815 | |
| 816 | var urlHandler = new config.urlCreatorFactory[config.urlCreator](); |
| 817 | urlHandler.setConfig(config); |
| 818 | |
| 819 | function restangularizeBase(parent, elem, route, reqParams, fromServer) { |
| 820 | elem[config.restangularFields.route] = route; |
| 821 | elem[config.restangularFields.getRestangularUrl] = _.bind(urlHandler.fetchUrl, urlHandler, elem); |
| 822 | elem[config.restangularFields.getRequestedUrl] = _.bind(urlHandler.fetchRequestedUrl, urlHandler, elem); |
| 823 | elem[config.restangularFields.addRestangularMethod] = _.bind(addRestangularMethodFunction, elem); |
| 824 | elem[config.restangularFields.clone] = _.bind(copyRestangularizedElement, elem, elem); |
| 825 | elem[config.restangularFields.reqParams] = _.isEmpty(reqParams) ? null : reqParams; |
| 826 | elem[config.restangularFields.withHttpConfig] = _.bind(withHttpConfig, elem); |
| 827 | elem[config.restangularFields.plain] = _.bind(stripRestangular, elem, elem); |
| 828 | |
| 829 | // Tag element as restangularized |
| 830 | elem[config.restangularFields.restangularized] = true; |
| 831 | |
| 832 | // RequestLess connection |
| 833 | elem[config.restangularFields.one] = _.bind(one, elem, elem); |
| 834 | elem[config.restangularFields.all] = _.bind(all, elem, elem); |
| 835 | elem[config.restangularFields.several] = _.bind(several, elem, elem); |
| 836 | elem[config.restangularFields.oneUrl] = _.bind(oneUrl, elem, elem); |
| 837 | elem[config.restangularFields.allUrl] = _.bind(allUrl, elem, elem); |
| 838 | |
| 839 | elem[config.restangularFields.fromServer] = !!fromServer; |
| 840 | |
| 841 | if (parent && config.shouldSaveParent(route)) { |
| 842 | var parentId = config.getIdFromElem(parent); |
| 843 | var parentUrl = config.getUrlFromElem(parent); |
| 844 | |
| 845 | var restangularFieldsForParent = _.union( |
| 846 | _.values(_.pick(config.restangularFields, ['route', 'singleOne', 'parentResource'])), |
| 847 | config.extraFields |
| 848 | ); |
| 849 | var parentResource = _.pick(parent, restangularFieldsForParent); |
| 850 | |
| 851 | if (config.isValidId(parentId)) { |
| 852 | config.setIdToElem(parentResource, parentId, route); |
| 853 | } |
| 854 | if (config.isValidId(parentUrl)) { |
| 855 | config.setUrlToElem(parentResource, parentUrl, route); |
| 856 | } |
| 857 | |
| 858 | elem[config.restangularFields.parentResource] = parentResource; |
| 859 | } else { |
| 860 | elem[config.restangularFields.parentResource] = null; |
| 861 | } |
| 862 | return elem; |
| 863 | } |
| 864 | |
| 865 | function one(parent, route, id, singleOne) { |
| 866 | var error; |
| 867 | if (_.isNumber(route) || _.isNumber(parent)) { |
| 868 | error = 'You\'re creating a Restangular entity with the number '; |
| 869 | error += 'instead of the route or the parent. For example, you can\'t call .one(12).'; |
| 870 | throw new Error(error); |
no outgoing calls
no test coverage detected