Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To close issue #70 GoogleWebComponents/google-apis #71

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 24 additions & 34 deletions google-client-loader.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@

<!--
Element for loading a specific client Google API with the JavaScript client library.

For loading `gapi.client` libraries

##### Example

<google-client-loader id="shortener"
name="urlshortener"
version="v1"></google-client-loader>

<script>
var shortener = document.getElementById('shortener');
shortener.addEventListener('google-api-load', function(event) {
Expand All @@ -32,7 +28,6 @@
});
});
</script>

@demo
-->
<dom-module id="google-client-loader">
Expand All @@ -44,32 +39,25 @@
<script>
(function() {
'use strict';

// Stores whether the API client is done loading.
var _clientLoaded = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you update the doc string to signify that this is a flag across instances? It's now a bit confusing that there is this static prop and an instance prop that are named the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually do we still need this?


// Loaders and loading statuses for all APIs, indexed by API name.
// This helps prevent multiple loading requests being fired at the same time
// by multiple google-api-loader elements.
var _statuses = {};
var _loaders = {};

Polymer({

is: 'google-client-loader',

/**
* Fired when the requested API is loaded. Override this name
* by setting `successEventName`.
* @event google-api-load
*/

/**
* Fired if an error occurs while loading the requested API. Override this name
* by setting `errorEventName`.
* @event google-api-load-error
*/

properties: {
/**
* Name of the API to load, e.g. 'urlshortener'.
Expand All @@ -79,46 +67,51 @@
* Explorer</a>.
*/
name: String,

/**
* Version of the API to load, e.g. 'v1'.
*/
version: String,

/**
* App Engine application ID for loading a Google Cloud Endpoints API.
*/
appId: String,

/**
* Root URL where to load the API from, e.g. 'http://host/apis'.
* For App Engine dev server this would be something like:
* 'http://localhost:8080/_ah/api'.
* Overrides 'appId' if both are specified.
*/
apiRoot: String,

/**
* Name of the event fired when API library is loaded.
*/
successEventName: {
type: String,
value: 'google-api-load'
},

/**
* Name of the event fired when there is an error loading the library.
*/
errorEventName: {
type: String,
value: 'google-api-load-error'
},
clientLoaded: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a doc string

type: Boolean,
value: false,
readOnly: true
},
// Used to fix events potentially being fired multiple times by
// iron-jsonp-library.
waiting: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this changed to public?

type: Boolean,
value: false,
readOnly: true
}
},

// Used to fix events potentially being fired multiple times by
// iron-jsonp-library.
_waiting: false,

observers: [
'_tryLoadingApi(waiting, clientLoaded, name, version)'
],
/**
* Returns the loaded API.
*/
Expand All @@ -130,18 +123,15 @@
return undefined;
}
},

/**
* Wrapper for `gapi.auth`.
*/
get auth() {
return gapi.auth;
},

_loadClient: function() {
gapi.load('client', this._doneLoadingClient.bind(this));
},

_handleLoadResponse: function(response) {
if (response && response.error) {
_statuses[this.name] = 'error';
Expand All @@ -151,12 +141,10 @@
this._fireSuccess();
}
},

_fireSuccess: function() {
this.fire(this.successEventName,
{ 'name': this.name, 'version': this.version });
},

_fireError: function(response) {
if (response && response.error) {
this.fire(this.errorEventName, {
Expand All @@ -169,34 +157,36 @@
'version': this.version });
}
},

_doneLoadingClient: function() {
_clientLoaded = true;
this._setClientLoaded(true);
// Fix for API client load event being fired multiple times by
// iron-jsonp-library.
if (!this._waiting) {
this._loadApi();
}
},

_createSelfRemovingListener: function(eventName) {
var handler = function () {
_loaders[this.name].removeEventListener(eventName, handler);
this._loadApi();
}.bind(this);

return handler;
},

_tryLoadingApi: function(waiting, clientLoaded, name, version) {
if (!waiting && clientLoaded && name && version) {
this._loadApi();
}
},
_loadApi: function() {
if (_clientLoaded && this.name && this.version) {
this._waiting = false;
this._setWaiting(false);
// Is this API already loaded?
if (_statuses[this.name] == 'loaded') {
this._fireSuccess();
// Is a different google-api-loader already loading this API?
} else if (_statuses[this.name] == 'loading') {
this._waiting = true;
this._setWaiting(true);
_loaders[this.name].addEventListener(this.successEventName,
this._createSelfRemovingListener(this.successEventName));
_loaders[this.name].addEventListener(this.errorEventName,
Expand All @@ -221,4 +211,4 @@
}
});
})();
</script>
</script>