kt3k / ajax-form.js

Automatic <form> element

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ajax-form.js v0.1.4

Automatic <form> element which creates an ajax request according to the state of the inputs. You don't need to write any JavaScript just for wiring your forms and apis.

Note: This lib depends on jQuery and qwest.

Install

bower install --save ajax-form.js

The markup

<form class="ajax-form">

  <input name="user">

  <input name="comment">

  <button
    data-api="/api/comments"
    data-method="PUT">Send</button>

</form>

This form creates PUT request to /api/comments with data user=[user] and comment=[comment] when the button is pushed. (events are automatically bound)

success / failure handling

<form class="ajax-form" id="my-form">

    ...
    ...

</form>


<script>

$(function () {

    $('#my-form').on('success.ajax-form', function () {

        // some success handling

    }).on('failure.ajax-form', function () {

        // some failure handling

    });

});

</script>

Manual initialization

.ajax-form elements are automatically initialized at $(document).ready. If you want to create .ajax-form elements after that, you can intialize them manually by triggering init.ajax-form event on document object.

createFormAsync().then(function () {

    $(document).trigger('init.ajax-form');

});

Similar projects

About

Automatic <form> element

License:MIT License


Languages

Language:JavaScript 100.0%