Created
February 18, 2014 01:21
-
-
Save robwormald/9062760 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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