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

elitejax

v2.0.3

Published

Simplifying Ajax Requests Using HTML attributes

Downloads

3

Readme

elitejax

Build Status Bower version

Simplifying Ajax Requests Using HTML attributes

Introduction


Elitejax is a standalone javascript library that makes AJAX requests a lot more easier without you writing a single line of javascript.

All you have to do is add data-elitejax="true" attribute to your form tag and you are good to go

Installation


You can install this library by cloning this git and reference the javascript files from the build directory, using npm:

npm install elitejax --save

or using bower:

bower install elitejax

Usage Without javascript

If your request returns data as JSON, you can add to the DOM from that object without javascript using the data-post and data-postTo attributes in your form element.

The data-post attribute is used to specify what part of the data returned you want to use. data-postTo is the DOM selector of where to post the resulting value.

<!-- using the spotify API -->
<div class="result"></div>
<form name="spotify" data-elitejax="true" action="https://api.spotify.com/v1/search" data-post="artists.items[1].name" method="get" data-postTo=".result" >
  Enter name: <input type="text" name="q"><br />
  Category: <input type="text" name="type" value="artist"><br />
  <input type="submit" value="Submit">
</form>
<script src="path/to/elitejax.min.js" charset="utf-8"></script>

The above will place the result in the div element with .result class.

Exclude form field


To exclude a form field in your form from your AJAX request, you can add data-elitejax-x attribute to that field, like:

<!-- below form field will be ignored -->
<input type="input" data-elitejax-x />

Adding custom configurations


Due to elitejax's flexibility, you can add custom configuration for each form in your webpage. First specify a name for your form and then use it with ej.configure, like so:

<form data-elitejax="true" name="spotify" action="https://api.spotify.com/v1/search" method="get">
.....

</form>
<!-- include elitejax library from bower components directory -->
<script src="bower_components/elitejax/build/elitejax.min.js" charset="utf-8"></script>
<script type="text/javascript">
    elitejax.configure('spotify'[, {configuration}]);
</script>

The configuration object argument for the configure method takes 4 parameters:

  • async default: true : You can set this to true or false

  • cType default: "application/json" : This is the content type header.

  • resType default: "json" : This is the response type of the AJAX query, you can use jsonp for cross domain requests.

  • callback default: function : The default callback logs the data to the console. You can specify your callback function for when the request completes successfully

Making Custom AJAX requests


You can use the elitejax ajaxIt method to send a custom AJAX request:

elitejax.ajaxIt(url, method, data[, requestName]);
  • url (string) : the API Endpoint or url the request is to be made to.
  • method (string) : can be get/post/delete/put request type.
  • data (object) : data you want to send to the url
  • requestName - optional (string) : The name you want to give to your AJAX request. You need to set this if you want to use custom configuration (the elitejax configure method) with your AJAX request.

So, we can customize the spotify API call to run on page load instead of depending on user interaction like so:

<script src="bower_components/elitejax/build/elitejax.min.js" charset="utf-8"></script>
<script type="text/javascript">
  window.onload = function () {
    var data = {
      q: 'Justin',
      type: 'artist'
    }
    elitejax.ajaxIt('https://api.spotify.com/v1/search', 'get', data, 'spotifyCustom');
  }
</script>

License

Elitejax is Licensed under the MIT License

Contributing

Please do! Send a pull request after your modifications.

Sharing is caring.... make sure to let your peers know.