$("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:
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.
$("form").autosave();
If you use this plugin as is (without providing any options), this is what you can expect.
change
default trigger, the scope of inputs is narrowed to include only those whose value has changed since the last autosave.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.
These are the arguments that are passed to trigger callback methods.
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.
These are the arguments that are passed to scope callback methods.
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.
These are the arguments that are passed to data callback methods.
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.
These are the arguments that are passed to condition callback methods.
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.
These are the arguments that are passed to save callback methods.
options.data
parameter to allow your callback method to take dynamically generated data (new data will be gathered upon every save attempt).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.
$("form").autosave().bind("saved", function(event) {
// ...
});
Triggered whenever autosave finishes saving form data.
This event is fired for each form autosave is attached to.
$("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.
$("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.
jQuery.autosave requires:
Verified to work correctly on:
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.