A simple javascript plugin to autosave content from form input elements or divs.
Starts with intialization of Options Options are
- autoSaveInterval This expects time interval in milliseconds. Each autosave action will be performed in this time interval if the content of any of the elements specified in 'monitorChangesOf' changes.
- autosaveUrl The data will submitted as POST to the url specified here.
- monitorChangesOf This expects a hash. The key of the hash will specify the name of the parameter while sending to server and the value of the hash expects the DOM element ID or class[which has to be unique] to be monitored for changes. eg: { description: "#my_article_description", title: ".my_article_title" } This means it will send the below object { "description" => content of "my_article_description"[id] element "title" => content of "my_article_title"[class] element } as POST in autosave action in the specified time interval only if the content of either one of them changes.
- extraParams This expects a hash. If you want some other extra params to be send to backend along with the data in the specified DOM elements, you can specify it here. This hash will be send along with the other specified DOM element data and if the response contains any JSON with a key matching any key in extraParams, that keys value will be updated as well. And next time this updated value will be send to the server.
- responseCallback This expects a function with a single parameter. This will be called after each autosave action with the response recieved from the server.
- minContentLength This is an integer value. This is used to check the content length of the elements monitored before submitting in autosave. So autosave happens only if the monitored elements content is greater than minContentLength. You have to enable minContentLengthCheck for this. It is enabled by default actually. Only if you pass minContentLength param as option will this work.
- retryIfError This is a flag. If an autosave encounters a failure, by default it will stop. But if you enable this flag, autosave will keep on trying submiting the data in specific intervals as specified.
Along with the above options, few flags and counters are available which are listed as follows.
- contentChanged This is flag, which is set to true if the content has changed in any of the fields which are monitored.
- savingContentFlag This flag is set to true while the saving process is going on.
- successCount This is counter. This keeps tracks of succesfull autosave attempts.
- failureCount This is a counter. This kepps tracks of failed autosave attempts.
- lastSaveStatus This is flag. This will be true if the last autosave attempt was succesfull or else false.
- minContentLengthCheck This is a flag. This has to be enabled so that each input which is monitored will be checked for a min content length. You have to pass an argument minContentLength in the opts if you enable this check.
Example
To autosave contents of a 'div' with 'id' "editable_content_div", you can initialize with the below options.
var draft_options = { autosaveInterval: 5000, autosaveUrl: "draft/autosave", monitorChangesOf: { content: "#editable_content_div" }, retryIfError: false };
window.emailAutosave = $.autoSaveContent(draft_options);