Skip to content

Commit 0e7f266

Browse files
committed
Separating out the loading of the core api library and the client library.
The client library is not needed by all components.
1 parent 906f6cf commit 0e7f266

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
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: 15 additions & 5 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,24 @@
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+
var self = this;
43+
gapi.load("client", function() {
44+
self.fire(self.notifyEvent, arguments);
45+
});
46+
},
47+
3848
/**
3949
* Fired when the API library is loaded and available.
4050
* @event api-load

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)