angular factory

(function(){ 'use strict'; angular.module('app') .factory('mainService', mainService); mainService.$inject = ['$http', '$q']; function mainService($http, $q){ var service ={ getData : getData, setData: setData, getRawList : getRawList, getList: getList, setList: setList }; var _self = this; _self.modelUrl = "model.php?"; _self.dataList = {}; return service; //************************* function callData(url, parameters){ var deferred = $q.defer(); $http.post(url, parameters).then(function(data){ deferred.resolve(data); }, function(data){ deferred.reject("error - no data from server" + data); console.log("error - no data from server" + data); }); return deferred.promise; } //************************* function getData(serverAction, ID){ ID = ID || false; var parameters = {action : serverAction, id: ID}; var promise = goCall(_self.modelUrl, parameters); return promise; } function setData(serverAction, data){ data.action = serverAction; var promise = goCall(_self.modelUrl, data); return promise; } function getRawList(serverAction){ var parameters = {action : serverAction}; var promise = callData(_self.modelUrl, parameters); return promise; } function getList(){ return _self.dataList; } function setList(list){ _self.dataList = list; } }//close factory })(); /*close function() wrapper*/

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.