npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

leaflet-ajax

v2.1.0

Published

AJAX and JSONP in Leaflet

Downloads

3,426

Readme

leaflet-ajax

js-semistandard-style

Allows you to call JSON via an Ajax call with a jsonp fallback.

var geojsonLayer = new L.GeoJSON.AJAX("geojson.json");

for jsonp add the option "dataType" and set it to "jsonp"

var geojsonLayer = L.geoJson.ajax("http:webhost.fake/geojson.jsonp",{dataType:"jsonp"});

Note that data starts to download when the layer is created NOT when it’s added to the map in order to get a head start.

You may pass either a url string or an array of url strings if you want to download multiple things (handy if your downloading data from an ESRI based thing which will have separate line, point, and poly features).

As you see you can also use lower case methods without creating new objects

For weirder jsonp you can set "callbackParam" for if you need to change the name of the callback parameter to something besides "callback", e.g. Mapquest Nominative Open uses "json_callback" instead of "callback".

If you want to be able to load stuff from the file system (with appropriate custom flags set) set local to true.

If you want to set headers to the XMLHttpRequest set the 'headers' option equal to an object.

Gives off three events data:loading, data:progress and data:loaded.

  • data:loading fires before we start downloading things, note if the constructor is given a url it won't wait to be added to the map to start downloading the data, but it does do an async wait so you have time to add a listener to it (and so leaflet.spin will work with it).
  • data:progress is called each time a file is downloaded and passes the downloaded geojson as event data.
  • data:loaded is called when all files have downloaded, this is mainly different from data:progress when you are downloading multiple things.

You can also add a middleware function which is called after you download the data but before you add it to leaflet:

var geojsonLayer = L.geoJson.ajax("route/to/esri.json",{
    	middleware:function(data){
        	return esri2geoOrSomething(json);
    	}
    });

addUrl does not clear the current layers but adds to the current one, e.g.:

var geojsonLayer = L.geoJson.ajax("data.json");
geojsonLayer.addUrl("data2.json");//we now have 2 layers
geojsonLayer.refresh();//redownload the most recent layer
geojsonLayer.refresh("new1.json");//add a new layer replacing whatever is there

last but now least we can refilter layers without re adding them

var geojsonLayer = L.geoJson.ajax("data.json");
geojsonLayer.refilter(function(feature){
    return feature.properties.key === values;
});

Behind the scenes are two new classes L.Util.ajax = function (url) for same origin requests and L.Util.jsonp = function (url,options) cross origin ones. Both return promises, which have an additional abort method that will abort the ajax request.

L.Util.ajax("url/same/origin.xml").then(function(data){
	doStuff(data);
});
//or
L.Util.jsonp("http://www.dif.ori/gin").then(function(data){
	doStuff(data);
});

In related news L.Util.Promise is now a constructor for a promise, it takes one argument, a resolver function.

Some of the jsonp code inspired by/taken from this interesting looking plugin that I have failed to make heads nor tails of (the plugin, not the jsonp code)