Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
pi-gfeeds loads the google feeds api only 1x now.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorvell committed Jan 17, 2014
1 parent c739674 commit 24a6b1a
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions elements/pi-gfeeds.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,53 @@
display: none;
}
</style>

<polymer-google-jsapi on-polymer-google-jsapi-loaded="{{jsapiLoaded}}"></polymer-google-jsapi>
</template>
<script>
(function() {
// TODO(ffu): integrate the ability to load a specific api like feeds
// into polymer-google-jsapi.
var feedsCallbacks = [];
var feedsApiLoading = false;

function feedsApiLoaded() {
while(feedsCallbacks.length) {
var fn = feedsCallbacks.shift();
fn();
}
}

function isApiLoaded() {
return window.google && google.feeds && google.feeds.Feed;
}

function withFeedsApi(callback) {
if (isApiLoaded() && callback) {
callback();
} else {
if (!feedsApiLoading) {
var loader = document.createElement('polymer-google-jsapi');
loader.addEventListener('polymer-google-jsapi-loaded', function() {
google.load('feeds', '1', {callback: feedsApiLoaded});
});
feedsApiLoading = true;
}
if (callback) {
feedsCallbacks.push(callback);
}
}
}

Polymer('pi-gfeeds', {
count: 24,
created: function() {
this.pendingFeeds = [];
},
ready: function() {
if (window.google && window.google.loader) {
this.jsapiLoaded();
}
},
jsapiLoaded: function() {
google.load('feeds', '1', {callback: this.feedsLoaded.bind(this)});
},
feedsLoaded: function() {
this.pendingFeeds.forEach(function(callback) {
callback();
});
this.pendingFeeds = [];
},
feedsReady: function(callback) {
if (window.google && window.google.feeds) {
callback();
} else {
this.pendingFeeds.push(callback);
}
},
feedChanged: function() {
if (this.feed) {
this.loading = true;
// call fetchFeeds async to make sure any binding to count property is resolved
// before fetchFeeds is called
this.feedsReady(this.asyncMethod.bind(this, 'fetchFeeds'));
withFeedsApi(this.fetchFeeds.bind(this));
} else {
this.results = null;
}
Expand Down Expand Up @@ -88,5 +99,6 @@
}
}
});
})();
</script>
</polymer-element>

0 comments on commit 24a6b1a

Please sign in to comment.