Skip to content

Commit 96fd651

Browse files
committed
Merge pull request #39 from Zoramite/api-js
Separating out the loading of the core api library and the client
2 parents 4f88265 + e162fc4 commit 96fd651

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

demo.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,19 @@
2525
console.log(e.target.localName + ' loaded');
2626
});
2727
</script>
28+
29+
<google-api-loader id="shortener" name="urlshortener"
30+
version="v1"></google-api-loader>
31+
<script>
32+
var shortener = document.getElementById('shortener');
33+
shortener.addEventListener('google-api-load', function(event) {
34+
var request = shortener.api.url.get({
35+
shortUrl: 'http://goo.gl/fbsS'
36+
})
37+
request.execute(function(resp) {
38+
console.log(resp);
39+
});
40+
});
41+
</script>
2842
</body>
2943
</html>

google-client-api.html

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
-->
99

1010
<link rel="import" href="../polymer/polymer.html">
11-
<link rel="import" href="../core-shared-lib/core-shared-lib.html">
11+
<link rel="import" href="google-js-api.html">
1212

1313
<!--
1414
Dynamically loads the Google Client JavaScript API, firing the `api-load` event when ready.
@@ -27,14 +27,23 @@
2727
</script>
2828
2929
@element google-client-api
30-
@extends core-shared-lib
3130
@homepage https://googlewebcomponents.github.io/google-apis
3231
-->
33-
<polymer-element name="google-client-api" extends="core-shared-lib">
32+
<polymer-element name="google-client-api">
33+
<template>
34+
<google-js-api on-js-api-load="{{loadClient}}"></google-js-api>
35+
</template>
3436
<script>
3537
Polymer({
36-
defaultUrl: 'https://apis.google.com/js/client.js?onload=%%callback%%',
37-
38+
/**
39+
* Loads the client library when the api js has loaded.
40+
*/
41+
loadClient: function() {
42+
gapi.load("client", function() {
43+
this.fire(this.notifyEvent, arguments);
44+
}.bind(this));
45+
},
46+
3847
/**
3948
* Fired when the API library is loaded and available.
4049
* @event api-load
@@ -224,11 +233,10 @@
224233
},
225234

226235
createSelfRemovingListener: function(eventName) {
227-
var self = this;
228236
var handler = function () {
229-
loaders_[self.name].removeEventListener(eventName, handler);
230-
self.loadApi();
231-
};
237+
loaders_[this.name].removeEventListener(eventName, handler);
238+
this.loadApi();
239+
}.bind(this);
232240

233241
return handler;
234242
},

google-js-api.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!--
2+
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
3+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6+
Code distributed by Google as part of the polymer project is also
7+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
8+
-->
9+
10+
<link rel="import" href="../polymer/polymer.html">
11+
<link rel="import" href="../core-shared-lib/core-shared-lib.html">
12+
13+
<!--
14+
Dynamically loads the Google JavaScript API, firing the `api-load` event when ready.
15+
16+
Any number of components can use `<google-js-api>` elements, and the library will only be loaded once.
17+
18+
##### Example
19+
20+
<google-js-api></google-js-api>
21+
<script>
22+
var api = document.querySelector('google-js-api');
23+
api.addEventListener('js-api-load', function(e) {
24+
console.log('API loaded');
25+
});
26+
</script>
27+
28+
@element google-js-api
29+
@extends core-shared-lib
30+
@homepage https://googlewebcomponents.github.io/google-apis
31+
-->
32+
<polymer-element name="google-js-api" extends="core-shared-lib">
33+
<script>
34+
Polymer({
35+
defaultUrl: 'https://apis.google.com/js/api.js?onload=%%callback%%',
36+
37+
/**
38+
* Fired when the API library is loaded and available.
39+
* @event js-api-load
40+
*/
41+
notifyEvent: 'js-api-load'
42+
});
43+
</script>
44+
</polymer-element>

0 commit comments

Comments
 (0)