UNPKG

hbp-react-ui

Version:

A library of useful user-interface components built with React and MobX

52 lines (39 loc) 2.76 kB
'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.default = undefined;var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}(); /////////////////////////////////////////////////////////// // File : Store.js // Description : /** * Store is an abstract class that asynchronously queries an endpoint. To use this functionality, derive a class that provides a 'URL' property. * @class Store * @property {string} endpointURL The GET endpoint to retrieve the data */ // Imports : var _axios = require('axios');var _axios2 = _interopRequireDefault(_axios);function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}} _axios2.default.defaults.xsrfCookieName = 'csrftoken'; _axios2.default.defaults.xsrfHeaderName = 'X-CSRFToken'; // Class Definition var Store = function () { // Constructor // Attributes function Store(endpointURL) {_classCallCheck(this, Store);this.URL = endpointURL; } // Operations _createClass(Store, [{ key: 'queryData', value: function queryData(updateObserver) { if (typeof _hbp_debug_ != 'undefined') console.log('Store.queryData'); try { if (typeof _hbp_debug_ != 'undefined') console.log('XHR(GET) Request: ' + this.URL); _axios2.default.get(this.URL) // Will do the request asynchronously so that the UI is still responsive .then(function (response) { if (typeof _hbp_debug_ != 'undefined') console.log('XHR(GET) Succeeded: ' + response.status.toString()); this.data = response.data; updateObserver(); }.bind(this)) // Needs to be bound otherwise 'this' is undefined // Change to 'arrow' syntax to do this automatically .catch(function (error) { console.log('XHR(GET) Failed: ' + error); }.bind(this)); } catch (error) { console.log('Store.queryData: ' + error.message); } } }]);return Store;}(); // Exports exports.default = Store;