evoapp.service('dataService', function ($rootScope, $http, $q, $timeout, modelService, $filter) { var serviceBase = window.evoapp.CTXPATH;//'http://java1.evo.pt:8080/siprp_fichasclinicas/rs/fichasclinicas/'; var record = function(model){ var record = arguments[1]; record = record == undefined ? {} : record; record = modelService.toModel(model, record); return record; }; var store = function(){ var me = this, args = arguments[0]; angular.copy(args, me); this.items = []; this.start = (this.start == null || this.start == undefined) ? 0 : this.start; this.limit = (this.limit == null || this.limit == undefined) ? Number.MAX_VALUE : this.limit; this.sort = (this.sort == null || this.sort == undefined) ? null : this.sort; this.dir = (this.dir == null || this.dir == undefined) ? 'ASC' : this.dir; this.total = 0; var params = null; this.extraParams = null; this.convertToModel = true; this.broadcastEvents = true; $http.defaults.withCredentials = true; this.get = function(){ var callbackFn = arguments[0]; params = {start: me.start, limit: me.limit, sort: me.sort, dir: me.dir}; if(me.extraParams != null) { for(property in me.extraParams) { params[property] = me.extraParams[property]; } //TODO: we need to review this. maybe add a model to it? //format dates for server for(property in params) { var value = params[property]; if(angular.isDate(value)) { //all dates going to server must be ISO8601 params[property] = moment(value).format('YYYY-MM-DD HH:mm:ss'); } } } $http({ method: 'GET', url: (serviceBase + me.actions.get), params: params, $$broadcastEvents: me.broadcastEvents }). success(function(response, status, headers, config) { //processServerDate(response); if(response.success) { //response.data = me.convertToModel ? modelService.collectionToModel(me.model, response.data, null) : response.data; me.total = response.total; me.items = response.data; } if(callbackFn) { callback(callbackFn, [response, status, headers, config, me.items]); } }). error(function(response, status, headers, config) { if(me.broadcastEvents) { $rootScope.$broadcast('response', {response: response}); } callback(callbackFn, [response, status, headers, config]); }); }; this.upsert = function(selected, editing){ var modelInstance = evoapp.models[me.model], callbackFn = arguments[2], //angular.isFunction(arguments[1]) ? arguments[1] : null; isNewRecord = false; //var hashKey = selected['$$hashKey'] != undefined ? selected['$$hashKey'] : null; //var isArray = angular.isArray(editing); //if(isArray) //{ // editing = modelService.collectionToModel(me.model, editing, 'upsert'); //} //else //{ // editing = modelService.toModel(me.model, editing, 'upsert'); //} $http({ method: 'POST', url: (serviceBase + me.actions.upsert), data: editing, $$broadcastEvents: me.broadcastEvents }). success(function(response, status, headers, config) { if(me.broadcastEvents) { $rootScope.$broadcast('response', {response: response}); } if(response.success) { if(response.data != null) { angular.copy(response.data, editing); angular.copy(response.data, selected); } } callback(callbackFn, [response, selected, editing, isNewRecord]); }). error(function(response, status, headers, config) { if(me.broadcastEvents) { $rootScope.$broadcast('response', {response: response}); } callback(callbackFn, [response, selected, editing, isNewRecord]); }); }; this.destroy = function(editing){ var modelInstance = evoapp.models[me.model], callbackFn = arguments[1]; //isArray = angular.isArray(editing); //if(isArray) //{ // editing = modelService.collectionToModel(me.model, editing, null); //} //else //{ // editing = modelService.toModel(me.model, editing, null); //} $http({ method: 'POST', url: (serviceBase + me.actions.destroy), data: editing, $$broadcastEvents: me.broadcastEvents }). success(function(response, status, headers, config) { if(me.broadcastEvents) { $rootScope.$broadcast('response', {response: response}); } callback(callbackFn, [response]); }). error(function(response, status, headers, config) { if(me.broadcastEvents) { $rootScope.$broadcast('response', {response: response}); } callback(callbackFn, [response]); }); }; this.loadData = function(data){ for(n = 0; n < data.length; n++) { this.items.push(data[n]); } }; this.refresh = function(){ me.get(); }; }; //pass specified values to specified function arguments function callback(fn, args) { var fnArgs = [].concat(args); fnArgs.push.apply(fnArgs, arguments); return fn.apply(this, fnArgs); }; return { store: store, record: record }; })