Giter VIP home page Giter VIP logo

elitejax's Introduction

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.

elitejax's People

Contributors

ghostffcode avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.