index.js (1223B)
1 import L from 'leaflet'; 2 import {fetch} from '~/lib/xhr-promise'; 3 import {notify} from '~/lib/notifications'; 4 import * as logging from '~/lib/logging'; 5 6 L.Layer.GeoJSONAjax = L.GeoJSON.extend({ 7 options: { 8 requestTimeout: 30000 9 }, 10 11 initialize: function(url, options) { 12 L.GeoJSON.prototype.initialize.call(this, null, options); 13 this.url = url; 14 }, 15 16 // Promise can be rejected if json invalid or addData fails 17 loadData: function() { 18 if (this._loadStarted) { 19 return; 20 } 21 this._loadStarted = true; 22 fetch(this.url, {timeout: this.options.requestTimeout}) 23 .then( 24 (xhr) => { 25 this.addData(JSON.parse(xhr.response)); 26 }, 27 (e) => { 28 logging.captureException(e, 'failed to get geojson'); 29 notify(`Failed to get GeoJSON data from ${this.url}: ${e.message}`); 30 } 31 ); 32 }, 33 34 onAdd: function(map) { 35 L.GeoJSON.prototype.onAdd.call(this, map); 36 this.loadData(); 37 } 38 } 39 ); 40