├── angular-odata.js └── README.md /angular-odata.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | "use strict"; 3 | 4 | /** 5 | * This module provides support for odata requests when using $resource in angular js. 6 | * Copyright (c) 2014 Code Fabriek 7 | */ 8 | var odata = angular.module("codefabriek.odata", []); 9 | 10 | /** 11 | * Configure the odata settings. 12 | */ 13 | odata.provider("$odata", [function () { 14 | var $routePrefix; 15 | 16 | this.routePrefix = function (prefix) { 17 | if (prefix) { 18 | $routePrefix = prefix; 19 | return this; 20 | } 21 | 22 | return $routePrefix; 23 | }; 24 | 25 | this.$get = [function () { 26 | return { 27 | routePrefix: this.routePrefix() 28 | } 29 | }]; 30 | }]); 31 | /** 32 | * Handles the outgoing $resource requests and transforms the parameters to odata compatible format. 33 | * The build-in $odataProvider is used to intercept requests to url's that contain the configured route prefix. 34 | */ 35 | odata.factory("odataInterceptor", ["$odata", function ($odata) { 36 | if (!$odata || !angular.isString($odata.routePrefix)) 37 | throw new Error("no route prefix is specified!"); 38 | 39 | var odataInterceptorFactory = {}; 40 | 41 | var _request = function (config) { 42 | if (config.method === "GET" && config.url.indexOf($odata.routePrefix) !== -1) { 43 | var params = {}; 44 | 45 | for (var prop in config.params) { 46 | var value = config.params[prop]; 47 | 48 | if (angular.isDefined(value)) 49 | params["$" + prop] = value; 50 | } 51 | 52 | config.params = params; 53 | } 54 | 55 | return config; 56 | }; 57 | 58 | odataInterceptorFactory.request = _request; 59 | 60 | return odataInterceptorFactory; 61 | }]); 62 | })(); 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Angular-odata 2 | ============= 3 | 4 | This module converts *GET* request parameters to the odata format when using $resource. 5 | 6 | Configuration 7 | ============= 8 | Please register your route prefix in the module.config method. 9 | 10 | var app = angular.modulde("app",["ngResource","codefabriek.odata"]); 11 | app.config(["$httpProvider","$odataProvider",function($httpProvider, $odataProvider){ 12 | $odataProvider.routePrefix("/odata/"); 13 | $httpProvider.interceptors.push("odataInterceptor"); 14 | }]); 15 | 16 | The *GET* request parameters to url's that contain the configured prefix will be converted to odata format. 17 | 18 | How to 19 | ============= 20 | 21 | app.factory("fakeResource",["$resource", function($resource){ 22 | return $resource("/api/odata/Product",{ 23 | format: "@format" 24 | }); 25 | }]); 26 | app.controller("fakeController",["fakeResource",function(fakeResource){ 27 | $scope.products = []; 28 | fakeResource.query({format: "application/json"}, function(response){ 29 | $scope.products = response; 30 | }); 31 | }]); 32 | 33 | the actuel GET url will be transformed from: 34 | 35 | http://localhost/api/odata/Product?format=application/json 36 | 37 | **to** 38 | 39 | http://localhost/api/odata/Product?$format=application/json 40 | 41 | MIT license 42 | ============================= 43 | Copyright (c) 2015 Code Fabriek 44 | 45 | Permission is hereby granted, free of charge, to any person 46 | obtaining a copy of this software and associated documentation 47 | files (the "Software"), to deal in the Software without 48 | restriction, including without limitation the rights to use, 49 | copy, modify, merge, publish, distribute, sublicense, and/or sell 50 | copies of the Software, and to permit persons to whom the 51 | Software is furnished to do so, subject to the following 52 | conditions: 53 | 54 | The above copyright notice and this permission notice shall be 55 | included in all copies or substantial portions of the Software. 56 | 57 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 58 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 59 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 60 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 61 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 62 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 63 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 64 | OTHER DEALINGS IN THE SOFTWARE. 65 | 66 | --------------------------------------------------------------------------------