CoffeeScript implementation of autosave for any input field using the browsers own localStorage object.
Just run "rake" or "make".
Using on the Browser side at page load time
You'll need both JQuery and autosave.js:
Both the title and body fields will be autosaved... if the form is submitted the autosave value will be cleared... and if an element with the behavior "clear" is clicked then the autosave value will also be cleared.
Using via AJAX with Rails, etc
You're new email RJS template might look like this:
page.replace_html :blankdiv, :partial => "emails/new" page << "AutoSave.enable($jQuery('#blankdiv'))"
After the content is loaded you have to throw the parent object to AutoSave so it can search for any new fields that it needs to enable AutoSave on.
Defining a unique hash key
Since localStorage is a simple key based storage system we'll need a different storage key for every single location inside your app where you want to enable autosave. It's up to you to figure this out based on your own needs, but the default implementation builds the key by joining all these pieces of info:
- current_user (if available via meta tag - see below)
- name attribute of the input/textarea tag
- the action attribute of the parent form
- the current browser URL
Including your apps unique user ID in the hash
<meta name="current_user" content="<%= current_user.try(:id) %>">
This would prevent two users on the same computer from ever seeing each others auto-complete text, even if they were working on the same pages and objects in your app.
Wrapping this is a quick class
I define a default Meta class that looks like this. This is entirely optional and if you don't like it feel free to yank it and inline that code inside the AutoSave class itself.
class Meta cu = $("meta[name=current_user]") @current_user = if cu then cu.content else null
And inside autosave.coffee you would access current user via:
You app might not even need current user as part of the hash... tweak to your own liking. The above is just the default.