UNPKG

noinfopath-odata

Version:

noinfopath odata module

163 lines (129 loc) 3.91 kB
/** * noinfopath-odata * @version 0.0.7 */ (function(angular, undefined){ "use strict"; angular.module('noinfopath.odata',['noinfopath.helpers']) .service("noODataQueryBulder",['$filter',function($filter){ /** * If value is a string then the value is returned * with single quotes around it. This ensures compatibility * the ODATA specification. * @param {string|number} value this is what should be wrapped if a string * @return {string|number} Either returns the original or a string wrapped in single quotes */ this.normalizeValue = function(value){ if(typeof value === "string"){ return "'" + value + "'"; }else if(angular.isDate(value)){ return $filter("date")(value, "DateTime'yyyy-MM-ddT0hh:mm:ss'"); }else{ return value; } }; this.makeFilterExpression = function(filterObj, processor){ console.log(angular.toJson(filterObj)) var filter = "$filter="; if(angular.isObject(filterObj)){ console.log("TODO: implement complex queries") filter = processor(filterObj); }else{ //Assume primative string, or number as passed in as filterObj. //in this case return a simple identity filter filter = "(" + this.normalizeValue(filterObj) + ")"; } return filter; }; this.makeSelectExpression = function(colmnsArray){ var select = "$select="; return select; }; this.buildQueryString = function(filter, mapParams, type){ var query; //function _makeFilter if(!filter){ query = ""; }else if(angular.isObject(filter)){ if(!angular.isFunction(mapParams)) throw "Parameter mapper required." console.log("TODO: Do something with a query that is an object."); query = "?" + mapParams(filter, type); }else{ query = "(" + queryBuilder.normalizeValue(filter) + ")"; } return query; } return this; }]) .provider("noODATA",[function(){ var PROV = this, endPoint = ""; function _noODATA($q, $http, queryBulder){ var SELF = this; this.makeResourceUrl = function(listname){ return PROV.getEndPoint() + "/" + listname; }; this.create = function(resourceURI, formdata){ var json = angular.toJson(formdata); var deferred = $q.defer(), req = { method: "POST", url: resourceURI, data: json, headers: { "Content-Type": "application/json", "Accept": "application/json" }, withCredentials: true }; $http(req) .success(function(data){ //console.log(angular.toJson(data) ); deferred.resolve(data.d); }) .error(function(reason){ deferred.reject(reason); }); return deferred.promise; } this.read = function(resourceURI, query){ //console.log(!!query); var deferred = $q.defer(), url = resourceURI + (!!query ? query : ""), req = { method: "GET", url: url, headers: { "Content-Type": "application/json", "Accept": "application/json" }, withCredentials: true }; console.log('\n' + url); $http(req) .success(function(data){ //console.log( angular.toJson(data)); deferred.resolve(data.value); }) .error(function(reason){ deferred.reject(reason); }); return deferred.promise; } this.update = function(){ var deferred = $q.defer(); return deferred.promise; } this.destroy = function(){ var deferred = $q.defer(); return deferred.promise; } } this.setEndPoint = function(uri) { endPoint = uri; }; this.getEndPoint = function() { return endPoint; }; this.$get = ['$q', '$http', 'noODataQueryBulder', function($q, $http, queryBuilder){ return new _noODATA($q, $http, queryBuilder); }]; }]) ; })(angular)