-
-
Save brigand/0dfcc7e48a2523606e1f 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
'use strict'; | |
app.factory('Gapi', ['$q', function($q) { | |
var loaded = false, loading = false; | |
function makeRequest() { | |
// do it later if we're not loaded yet | |
if (!loaded) { | |
return load().then(makeRequest); | |
} | |
var request = gapi.client.urlshortener.url.get({ | |
'shortUrl': 'http://goo.gl/fbsS' | |
}); | |
request.execute(function(response) { | |
appendResults(response.longUrl); | |
}); | |
console.log('inside make request'); | |
} | |
function appendResults(text) { | |
var results = document.getElementById('results'); | |
results.appendChild(document.createElement('P')); | |
results.appendChild(document.createTextNode(text)); | |
} | |
function load() { | |
if (loading) { | |
return loading; | |
} | |
var defered = $q.defer(); | |
gapi.client.setApiKey(''); | |
gapi.client.load('urlshortener', 'v1', function(){ | |
loaded = true; | |
defered.resolve(gapi); | |
}); | |
loading = defered.promise; | |
return defered.promise; | |
} | |
return { | |
loadgoogle: function() { | |
return load(); | |
} | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment