$("form").autosave();
The jQuery.autosave plugin automatically and unobtrusively saves form data based on a set of critera. Saving can be broken down into a simple five step process:
- A trigger callback begins the saving process.
- The scope of form inputs is narrowed appropriately.
- A dataset is created using those inputs.
- The current state of the plugin is tested against a series of conditions.
- If those conditions pass, we save the data using any number of methods.
This plugin works strictly with forms and form inputs of any type. Any other elements fed to the plugin will be ignored. Currently, if you wish to autosave data on a per form basis, you should attach a separate instance of the plugin to each form.
Note: the actual autosave instance (which is stored using jQuery's .data() function) is only attached to form elements, even if those element weren't passed in directly.
{
namespace: "autosave",
callbacks: {
trigger: "change",
scope: null,
data: "serialize",
condition: null,
save: "ajax"
},
events: {
save: "save",
saved: "saved",
changed: "changed",
modified: "modified"
}
}
Options is a set of key/value pairs that can be passed into the plugin as the first argument upon initialization. The default values are shown above.
- namespace String
The namespace to append after event names and before class names that are used within the plugin. This will also be the key name for the autosave instance stored in each element's expando data. - callbacks Object
Contains a set of key/value pairs that define callback methods for the autosave process described above.- trigger String, Object, Array, function
The callback method(s) that will start the saving process. - scope String, Object, Array, function, Boolean
The callback method(s) that will determine the scope of inputs from which to gather data. - data String, Object, Array, function, Boolean
The callback method(s) that will determine how to build the dataset from the inputs. - condition String, Object, Array, function, Boolean
The callback method(s) that will determine whether or not to save based on the current state of the plugin. - save String, Object, Array, function
The callback method(s) that will determine how the data will be saved.
- trigger String, Object, Array, function
- events Object
Contains a set of key/value pairs that allow you to change the name of events used within the plugin. Keep in mind that these events will be namespaced on initialization like: "eventName.namespace"- save String
This event will attempt to save anytime it is fired. It is bound to each form passed into the plugin on initialization. - saved String
This event is triggered on each form whenever autosave finishes saving form data. It can be bound to if you need to be notified after saving is completed. - changed String
This event is triggered whenever an input value changes ("change" event is fired) on the form containing that input. It can be bound to if you need to be notified whenever an input value changes. - modified String
This event is triggered whenever an input value is modified ("input" or "keyup" event is fired) on the form containing that input. It can be bound to if you need to be notified whenever an input value is modified.
- save String
$("form").autosave();
If you use this plugin as is (without providing any options), this is what you can expect.
- trigger An autosave is triggered any time an input value changes.
- scope Due to the
change
default trigger, the scope of inputs is narrowed to include only those whose value has changed since the last autosave. - data Data is gathered using jQuery's .serialize() function.
- condition There are no conditions that need to pass to complete this save.
- save The data is sent to the current browser URL using the jQuery.ajax() function.
Callback methods are invoked by the plugin during the autosave process. There are several built-in callback methods that provide you an easy way to set up the most common saving processes, but you can also write your own.
$("form").autosave({
// In all of these cases, "callbackName" refers to any of the callback
// options outlined above (trigger, scope, data, condition, save).
callbacks: {
// The simplest way to use a built-in callback is to pass in the
// method name as a string. Default callback options will be used.
callbackName: "callbackMethod",
// You may also pass in the method name as a string and provide
// custom options by using an object.
callbackName: {
method: "callbackMethod",
options: {
// ...
}
},
// An array of callbacks may also be provided.
callbackName: [
"callbackMethod1",
{
method: "callbackMethod2",
options: {
// ...
}
}
],
// The simplest way to use a custom callback method is to pass
// in a function. The arguments provided to this function vary
// depending on the callback used.
callbackName: function() {
// ...
},
// You may also pass in the method and custom options by using
// an object.
callbackName: {
method: function(options) {
// ...
},
options: {
// ...
}
}
}
});
The names of these methods are detailed below along with the arguments that will be passed in when the methods are invoked. If an Array of callback methods is provided, they will be invoked in the order they were defined in the Array. Every callback method is called with the current instance as the context of the keyword this making it easy to call any class property or function from within the callback method.
$("form").autosave({
callbacks: {
trigger: function(options) {
// ...
}
}
});
The built-in callback methods for triggering an autosave.
- change
Attempts to save any time an input value changes. - modify
Attempts to save any time an input value is modified. - interval
Creates an interval loop that will attempt to save periodically.
These are the arguments that are passed to trigger callback methods.
- options Object
An object of key/value pairs that may be used to configure the callback method.
Trigger methods do not require a return value.
$("form").autosave({
callbacks: {
scope: function(options, $inputs) {
// ...
// Must return a jQuery Object.
return $inputs;
}
}
});
The built-in callback methods for narrowing the scope of inputs we will gather data from.
- all
Uses all valid form inputs - changed
Filters inputs down to only those that have had their value changed since the last autosave. - modified
Filters inputs down to only those that have had their value modified since the last autosave.
These are the arguments that are passed to scope callback methods.
- options Object
An object of key/value pairs that may be used to configure the callback method. - $inputs jQuery
A jQuery object containing the current scope of inputs.
Scope methods should return a jQuery object containing the filtered inputs.
$("form").autosave({
callbacks: {
data: function(options, $inputs, formData) {
// ...
// Must return some kind of dataset.
return formData;
}
}
});
The built-in callback methods for generating data from the inputs.
- serialize
Serializes a set of form elements as a String using jQuery's .serialize() function. - serializeArray
Serializes a set of form elements as an Array using jQuery's .serializeArray() function. - serializeObject
Serializes a set of form elements as an Object of names and values using Ben Alman's .serializeObject() function.
These are the arguments that are passed to data callback methods.
- options Object
An object of key/value pairs that may be used to configure the callback method. - $inputs jQuery
A jQuery object containing the current scope of inputs. - formData String, Object, Array
Any data that has already been generated from previous data methods, or undefined if none have been called.
Data methods should return some kind of dataset, most likely containing the values from the inputs.
$("form").autosave({
callbacks: {
condition: function(options, $inputs, formData, caller) {
// ...
// Should return a boolean value.
// Returning boolean 'false' will cancel the save.
return false;
}
}
});
The built-in callback methods for determining whether or not to save.
- changed
Only save if at least one input value has changed since the last autosave. - modified
Only save if at least one input value has been modified since the last autosave. - interval
Only save on intervals. If anything else triggers an autosave, it will wait until the next interval to save.
These are the arguments that are passed to condition callback methods.
- options Object
An object of key/value pairs that may be used to configure the callback method. - $inputs jQuery
A jQuery object containing the current scope of inputs. The inputs given here have already been filtered. - formData String, Array, Object
The data gathered from the scoped form elements and returned from the data callback method. - caller String, Number
Used to denote who called the save method. This is generally undefined, but may contain the ID of the current interval timer or an event name.
Condition methods should return a Boolean value (true or false). Returning any non-false value is treated the same as returning true.
$("form").autosave({
callbacks: {
save: function(options, formData) {
// ...
// If your save function contains asynchronous code,
// you should return a Boolean 'false' here and call
// this.next("save") when your method has finished.
return false;
}
}
});
The built-in callback methods for determining how to save the input data.
- ajax
Will save the data using the jQuery.ajax() function.
These are the arguments that are passed to save callback methods.
- options Object
An object of key/value pairs that may be used to configure the callback method. Everything supported by the jQuery.ajax() function is supported here. Additionally, you may pass a function to theoptions.data
parameter to allow your callback method to take dynamically generated data (new data will be gathered upon every save attempt). - formData Object
The data gathered from the scoped form elements and returned from the data callback method.
Saving methods do not require a return value. However, if your callback method contains asynchronous code, such as an AJAX request, it must return false and contain a call to the function this.next("save") internally. The function this.next("save") tells the plugin the "save" callback has finished executing, allowing it to execute the next save method or perform necessary cleanup if there are no save methods left to execute.
For convenience, the plugin automatically binds or fires events on certain elements under certain circumstances. These events are listed below. Some of these events need to be triggered using jQuery's .triggerHandler() function on the element the event is bound to. Other events will be fired automatically and may be caught and handled using jQuery's .bind() function on the element firing the event. The jQuery Event Object will always be the first argument passed to handler methods. Also, keep in mind that these events will be namespaced according to the namespace option above ("autosave" by default).
$("form").autosave().bind("save", function(event, $inputs) {
// ...
}).triggerHandler("save");
When triggered, this event will attempt to save form data.
This event is bound to each form autosave is attached to.
- $inputs jQuery|Element|Element[]
The inputs to save. All inputs will be used by default.
$("form").autosave().bind("saved", function(event) {
// ...
});
Triggered whenever autosave finishes saving form data.
This event is fired for each form autosave is attached to.
- event Object
The jQuery.Event object.
$("form").autosave().bind("changed", function(event, input) {
// ...
});
Triggered whenever an input value changes ("change" event is fired).
This event is fired on the form containing the input.
- event Object
The jQuery.Event object. - input Element
The DOM element that triggered the event.
$("form").autosave().bind("modified", function(event, input) {
// ...
});
Triggered whenever an input value is modified ("keyup" event is fired).
This event is fired on the form containing the input.
- event Object
The jQuery.Event object. - input Element
The DOM element that triggered the event.
jQuery.autosave requires:
- jQuery version 1.4.3+
Verified to work correctly on:
- Chrome 7.0+
- Firefox 3.0+
- Internet Explorer 6.0+
Written by Kyle Florence and other contributers. Inspired by the jQuery.autosave plugin written by Raymond Julin, Mads Erik Forberg and Simen Graaten.
Copyright (C) 2012
Kyle Florence, Raymond Julin, Mads Erik Forberg and Simen Graaten.
jQuery.autosave is dual licensed under the BSD and MIT licenses.