Reputation: 1392
I have a page where i have to save some data [into the database] every few seconds.This is similar to how gmail or stackoverflow saves a draft every few seconds. I am using jQuery Ajax to achieve this. Following is my Ajax call :
Question: Is this the correct way to do this? I am not comfortable with opening and closing the connection every few seconds.
function ShowHtml() {
var SaveStoryForEditing = '<%= Page.ResolveUrl("~")%>Webservices/WebService.asmx/SaveStoryForEditing';
$('#savedata').html($('#InPlaceEdit').html());
$("#InPlaceEdit").find("textarea").each(function (idx) {
$("#savedata").find("textarea").eq(idx).text($(this).val());
});
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: SaveStoryForEditing,
dataType: 'json',
async: false,
data: "{'StoryEditId':" + $('#hfStoryEditID').val() + ",'AccountId':" + $('#hfAccID').val() + ",'StoryHtml':'" + $('#savedata').html() + "'}", // Page parameter to make sure we load new data
success: function (data) {
var myObject = eval('(' + data.d + ')');
$('#InPlaceEdit').effect("highlight", { color: "#ff0000" }, 1000);
$('#savedata').html('');
AutoSave();
},
error: function (result) {
AutoSave();
alert(JSON.stringify(result));
}
});
};
function AutoSave() {
setTimeout("ShowHtml()", 30000);
};
This is the function i am calling from the webservice:
Public Shared Function SaveStoryForEditing(ByVal StoryEditId As Integer, ByVal AccountId As Integer, ByVal StoryHtml As String) As Object
Dim db As SqlDatabase = Connection.Connection
Dim scalar As Object
Dim cmdIf As SqlCommand = CType(db.GetSqlStringCommand("UPDATE StoryEdit SET StoryHtml=@StoryHtml WHERE AccountID=@AccountID AND StoryEditID=@StoryEditID"), SqlCommand)
db.AddInParameter(cmdIf, "AccountID", DbType.Int32, AccountId)
db.AddInParameter(cmdIf, "StoryEditID", DbType.Int32, StoryEditId)
db.AddInParameter(cmdIf, "StoryHtml", DbType.String, StoryHtml)
scalar = db.ExecuteNonQuery(cmdIf)
Return scalar
End Function
This is my connection object class:
Imports Microsoft.Practices.EnterpriseLibrary.Data.Sql
Imports System.Data.SqlClient
Public Class Connection
Public Shared Function Connection() As SqlDatabase
Dim db As SqlDatabase = New SqlDatabase(System.Configuration.ConfigurationManager.ConnectionStrings("TripNestConStr").ConnectionString)
Return db
End Function
End Class
Upvotes: 0
Views: 759
Reputation: 4550
I am thinking why don't use the hidden field kind of flag to check if the value is updated or not. On keypressup of the textarea you can set the hidden value as true. In the autoSave method check the value of the hidden field , if it is true then only go back to server. Once you picked the value update it back to false again . So that it will help you to save some time overload . Another way is rather than saving true false , you can also save the time stamp of change and have a global variable in javascript which will tell the last time you have updated in the database. If the global value is in past compare to current update in the hidden field , you need to sync.
Upvotes: 1
Reputation: 13600
I believe this is not a good way to go. You're creating (probably) unnecessary traffic. If you really need such functionality, I'd at least consider some kind of mechanism that will track changes and execute ONLY if something has changed after the last server call. You don't really need to send a request to the server and access the database, if the data haven't changed.
Just some idea for you to consider, it all depends on your specific situation and needs.
Upvotes: 2