AngularJS: Variables "Global" to a Service -
i still working on first angularjs project (learning lot go mimicking existing app) , wos wondering best practices , better techniques.
i have service pulls form data sharepoint 365 list. mimicking example able grab data list. concern have duplication of code. every function declares same local variables , change function name , query there lot of duplicity. pulling on 30 lists , have keep duplicating crap until learn correctly.
for example, here 2 of "shorter" functions.
appiti.service('sharepointjsomservice', function($q, $http){ // priorities this.getpriorities = function(){ var deferred = $.deferred(); jsrequest.ensuresetup(); hostweburl = decodeuricomponent(jsrequest.querystring["sphosturl"]); appweburl = decodeuricomponent(jsrequest.querystring["spappweburl"]); var executor = new sp.requestexecutor(appweburl); executor.executeasync({ url: appweburl + "/_api/sp.appcontextsite(@target)/web/lists/getbytitle('itipriorities')/items?$select=id,title&@target='" + hostweburl + "'", method: "get", headers: { "accept": "application/json; odata=verbose" }, success: function(data, textstatus, xhr){ deferred.resolve(json.parse(data.body)); }, error: function(xhr, textstatus, errorthrown){ deferred.reject(json.stringify(xhr)); } }); return deferred; }; // /getpriorities // prioritygroups this.getprioritygroups = function(){ var deferred = $.deferred(); jsrequest.ensuresetup(); hostweburl = decodeuricomponent(jsrequest.querystring["sphosturl"]); appweburl = decodeuricomponent(jsrequest.querystring["spappweburl"]); var executor = new sp.requestexecutor(appweburl); executor.executeasync({ url: appweburl + "/_api/sp.appcontextsite(@target)/web/lists/getbytitle('itipriorities')/items?$select=id,priority_group&@target='" + hostweburl + "'", method: "get", headers: { "accept": "application/json; odata=verbose" }, success: function(data, textstatus, xhr){ deferred.resolve(json.parse(data.body)); }, error: function(xhr, textstatus, errorthrown){ deferred.reject(json.stringify(xhr)); } }); return deferred; }; // /getpriorities }
i don't know enough angularjs , promises know can change , can't learning. don't want learn wrong way.
any guidance appreciated!!!
i use single method reduce codebase. , closures metadata simplify creating service. like:
appiti.factory('sharepointjsonservice', function($q, $http,metadata){ var service = {}; angular.foreach(metadata,function(listdescriptor,listname){ //this === service this["get"+listname] = function(){ return get(listdescriptor); }; },service); function get(listdescriptor){ var deferred = $q.defer(); jsrequest.ensuresetup(); hostweburl = decodeuricomponent(jsrequest.querystring[listdescriptor.hosturl]); appweburl = decodeuricomponent(jsrequest.querystring[listdescriptor.appurl]); var executor = new sp.requestexecutor(appweburl); executor.executeasync({ url: appweburl + listdescriptor.path; method: listdescriptor.method, headers: listdescriptor.headers success: function(data, textstatus, xhr){ deferred.resolve(json.parse(data.body)); }, error: function(xhr, textstatus, errorthrown){ deferred.reject(json.stringify(xhr)); } }); return deferred.promise; } return service; });
you need create metadata object such satisfy code. like:
appiti.constant('metadata',{ priorities: { hosturl: ... appurl: ... method: "get", headers: ... etc.. }, prioritygroups:{ ... } });
you use angular promise $q
there no need $scope.$apply
in controller:
sharepointjsonservice.getpriorities().then( function success(res){ $scope.priorities = res.d.results; }, function error(){ //error });
Comments
Post a Comment