Skip to content

Instantly share code, notes, and snippets.

@LongLiveCHIEF
Last active August 29, 2015 13:56
Show Gist options
  • Save LongLiveCHIEF/9320683 to your computer and use it in GitHub Desktop.
Save LongLiveCHIEF/9320683 to your computer and use it in GitHub Desktop.
SharePoint site provider factory
.config(['spSiteProvider',function(spSiteProvider){
spSiteProvider.siteSvcUrl('https://myspsite.com/_vti_bin/listdata.svc')
}])
angular.module('ngd.SharePoint',[])
.provider('spSite',['$injector',function spSiteProvider($injector){
var siteSvcUrl;
//this allows us to set the siteSvcUrl at an app level, in case we are only using one spSite
//otherwise, instantiate the factory at a use-case level by passing the value to the spSite(svc) constructor and assigning the instance
//to a variable for usage within context
this.siteSvcUrl = function(url){
siteSvcUrl = url;
}
this.$get = ['$http',function spSiteFactory($http){
return $injector.instantiate(SpSite,{$http:$http,svc:siteSvcUrl})
}]
}])
function SpSite(svc){
var svcUrl = svc;
return {
getLists: function(){
return $http.get(svcUrl);
},
getListData: function(list,query){
var qry;
if(query){
qry = '?'+query;
}
return $http.get(svcUrl+'/'+list+qry);
},
create: function(data,query){
},
update: function(data,query){
}
}
}
.controller('theCtrl',['$http','spSite',function($http,spSite){
var splist = spSite.getListData('listA').then(function(data){
//do something with data
})
}])
@LongLiveCHIEF
Copy link
Author

the only problem I'm having is figuring out how to inject $http into the SpSite factory. Hmmm.

Tried injecting it in the provider, but $http is not available at that point in the application. The factory is configured to use case by case instead of application wide, and I haven't had much luck passing $http as a parameter for the factory. Wonder if defining the factory like:

 .factory('spSite',['$http`,function($http,svcUrl){
        //wonder if this might work?  Isn't this the injections method, not able to take custom args?

   }])

@LongLiveCHIEF
Copy link
Author

and tried adding as a prop on the SpSite function:

this.$inject = ['$http'];

@LongLiveCHIEF
Copy link
Author

Got it! took me long enough...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment