Reputation: 243
I am working on application where a a user can create a blog post and the blog post periodically updates every so often. I found a jQuery autosave plugin for handling the autosave but I am still running into problems.
When I debug using firebug I only see the GET request so therefore the page does not get updated and I am not sure how to call POST after the GET request.
It does not update as I type or every 3 seconds but it updates when I click outside of a form field. Anyway to have it so it updates every 3 seconds or so?
My code is listed below
application.js
jQuery(function($) {
$("#main-form").autosave({
callbacks: {
trigger: ["change", function() {
var self = this;
$("[name=save]").click(function() {
self.save();
});
}],
save: {
method: "ajax",
options: {
success: function() {
alert("saved!");
}
}
}
}
});
});
post_controller.rb
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to semester_post_path, notice: 'post was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
Upvotes: 3
Views: 2465
Reputation: 241
I'm sure you've moved on, but just to close the gap on this post. OnChange takes place when a field loses focus, so what you're seeing seems to be functioning as designed and normal for that design.
Here's a link to question/answer using timeouts to autosave via AJAX.
Autosave Opinion:
I'm not sure you really want envoke autosave until the document has been formally submitted for several reasons but I'll post my favorite 2.
Some forms tend to have a lot of moving parts for the back end storage and that first commit should be intentional before stuffing a bunch of data into tables. (IMHO)
another rationale behind leaving them alone (not auto-saving) before the first "real" submit is because until they hit submit they might not really want you to have that information in your system.
How often have you started a form on the web and then decided you didn't want to fill out that information after all? ... it would frustrate me to get a flyer or email or call from a site I actually decided to bail out of registration for!
Upvotes: 2