Skip to content

Instantly share code, notes, and snippets.

@robwormald
Created February 18, 2014 01:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwormald/9062760 to your computer and use it in GitHub Desktop.
Save robwormald/9062760 to your computer and use it in GitHub Desktop.
myApp.factory('dataThing',function($q,$http){
var thatData = null;
return {
getData : function(){
//create a promise
var deferredData = $q.defer()
//if we have the data already, just resolve with that
if(thatData){
deferredData.resolve(thatData)
}
//else get it from the server
else{
$http.get('/data.json').then(function(response){
thatData = response.data;
//resolve the promise w/ the data
deferredData.resolve(thatData)
})
}
//return the promise
return deferredData.promise;
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment