Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
152 lines (125 sloc) 4.25 KB
<!--
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../bower_components/polymer/polymer-element.html">
<dom-module id="shop-category-data">
<script>
(function() {
class ShopCategoryData extends Polymer.Element {
static get is() { return 'shop-category-data'; }
static get properties() { return {
categoryName: String,
itemName: String,
categories: {
type: Array,
notify: true
},
category: {
type: Object,
computed: '_computeCategory(categoryName, categories)',
notify: true
},
item: {
type: Object,
computed: '_computeItem(category.items, itemName, categories)',
notify: true
},
failure: {
type: Boolean,
notify: true,
readOnly: true
}
}}
_getCategoryObject(categoryName, categories) {
if ( !categories ) {
return {};
}
for (let i = 0, c; c = this.categories[i]; ++i) {
if (c.name === categoryName) {
return c;
}
}
}
_computeCategory(categoryName, categories) {
// Fetch the items of the category. Note that the fetch is asynchronous,
// which means `category.items` may not be set initially (but that path
// will be notified when the fetch completes).
let categoryObj = this._getCategoryObject(categoryName, categories);
this._fetchItems(categoryObj, 1);
return categoryObj;
}
_computeItem(items, itemName, categories) {
if (!items || !itemName || !categories) {
return;
}
for (let i = 0, item; item = items[i]; ++i) {
if (item.slug === itemName) {
return item;
}
}
}
_fetchCategories( attempts ) {
this._setFailure(false);
this._getResource({
url: 'https://us-central1-worlds-fastest-store-server.cloudfunctions.net/categories',
onLoad(e) {
this.set('categories', JSON.parse(e.target.responseText));
this.refresh();
},
onError(e) {
this._setFailure(true);
}
}, attempts);
}
_fetchItems(category, attempts) {
this._setFailure(false);
// Only fetch the items of a category if it has not been previously set.
if (!category || !category.id || category.items) {
return;
}
this._getResource({
url: 'https://us-central1-worlds-fastest-store-server.cloudfunctions.net/category/?category=' + category.id,
onLoad(e) {
this.set('category.items', JSON.parse(e.target.responseText));
},
onError(e) {
this._setFailure(true);
}
}, attempts);
}
_getResource(rq, attempts) {
let xhr = new XMLHttpRequest();
xhr.addEventListener('load', rq.onLoad.bind(this));
xhr.addEventListener('error', (e) => {
// Flaky connections might fail fetching resources
if (attempts > 1) {
this._getResourceDebouncer = Polymer.Debouncer.debounce(this._getResourceDebouncer,
Polymer.Async.timeOut.after(200), this._getResource.bind(this, rq, attempts - 1));
} else {
rq.onError.call(this, e);
}
});
xhr.open('GET', rq.url);
xhr.send();
}
constructor() {
super();
this._fetchCategories( 1 );
}
refresh() {
if (this.categoryName) {
// Try at most 3 times to get the items.
this._fetchItems(this._getCategoryObject(this.categoryName), 3);
}
}
}
customElements.define(ShopCategoryData.is, ShopCategoryData);
})();
</script>
</dom-module>
You can’t perform that action at this time.