kugaevsky / jquery-phoenix

Phoenix is a jQuery plugin that saves form state to Local Storage via HMTL5 Web Storage APIs
http://kugaevsky.github.io/jquery-phoenix/
MIT License
164 stars 29 forks source link

Phoenix Code Climate

jQuery plugin that saves form state to Local Storage via HMTL5 Web Storage APIs

Lost connections, crashed browsers, broken validations – all these shit lose forms data you've filled in with love and care.

Phoenix is a jQuery plugin that saves data entered inside form fields locally and restores it in case the browser crashes or the page is refreshed accidentally.

Phoenix keeps bad things away from your forms. It is very tiny – 3Kb (1Kb gziped), but powerful. Phoenix saves form fields values, checkboxes and radio button states to your browser Local Storage using HTML5 Web Storage API.

If at any point the Internet connection goes offline, the browser crashes, the page is refreshed or the form doesn't validate, the user filling in the form can restore the form to his last version instead of re-filling all the form fields again.

Enough words! Take a look at

Demo

Phoenix Demo

Try the DEMO!

Requirements

It's jQuery plugin so it requires jQuery.

Installation

Clone from source, download (minified) or install with bower.

Usage

  1. Require jQuery
  2. Require jquery.phoenix.js or its minified version
  3. $('input').phoenix() – cast the magic
  4. $('input').phoenix('remove') – clear storage on successful form submission (or any other event your like more)

Something like this

<form id='stored-form'>
  <input type="text" class="phoenix-input" />
  <textarea type="text" class="phoenix-input"></textarea>
</div>

<script type="text/javascript">
  $('.phoenix-input').phoenix()
  $('#my-form').submit(function(e){
    $('.phoenix-input').phoenix('remove')
  })
</script>

Do take a look at demo file source to understand usage.

API

Phoenix provides simple but flexible API.

Options

You can pass an options object on Phoenix initialization.

$('.phoenix-input').phoenix({
    namespace: 'phoenixStorage',
    webStorage: 'sessionStorage',
    maxItems: 100,
    saveInterval: 1000,
    clearOnSubmit: '#stored-form',
    keyAttributes: ['tagName', 'id']
  })

Possible options are:

Methods

When Phoenix initialized you can use API methods to manage it. Call method with $(selector).phoenix('methodName'), where methodName is one of these:

NB Phoenix doesn't remove stored item from localStorage by itself. So don't forget to call remove event when you don't need filled in form field values anymore or use clearOnSubmit option with form id.

Events

Every Phoenix method call fires an event you can bind to. For example

$('.phoenix-input').bind('phnx.loaded', function (e) {
  console.log('Data loaded... ')
})

Events naming is very obvious, so try them out

Compatibility

Browsers

Any compatible with HTML5 Web Storage API browser works well with Phoenix.

CanIuse cheatsheet

Other plugins

Dozens of plugins make form inputs more powerful and pretty. Chosen or Select2 for example. You can use it with Phoenix safely. Just make sure that you initialize Phoenix before these plugins.

Registries

License

Distributed under MIT license

Contributing

Feel free to create pull request of your changes. But only in CoffeeScript.

Almighty contributors.


Sponsored by Roem.ru Roem.ru