AJAX 自动保存功能

发布于 2024-07-22 20:36:05 字数 698 浏览 9 评论 0原文

实现自动保存功能的最佳 JavaScript 库、插件或扩展是什么?

具体需求是能够“保存”数据网格。 想想 gmail 和 Google 文档的自动保存功能。

如果轮子已经发明了,我不想重新发明。 我正在寻找神奇的 autoSave() 函数的现有实现。

自动保存:推送到保存到持久存储(通常是数据库)的服务器代码。 服务器代码框架超出了这个问题的范围。

请注意,我不是在寻找 Ajax 库,而是更高级别的库/框架:与表单本身交互。

daemach 引入了基于 jQuery @ http://daemach.blogspot 的实现。 de/2007/03/autosave-jquery-plugin.html [脚本主机关闭]。 但我不相信它符合轻量级和精心设计的标准。

标准

  • 稳定、轻量级、精心设计的
  • 保存 onChange 和/或 onBlur
  • 保存的频率不会超过给定的毫秒数
  • 处理同时发生的多个更新
  • 如果自上次保存以来没有发生任何更改,则不会保存
  • 每个输入类保存到不同的 url

What's the best javascript library, or plugin or extension to a library, that has implemented autosaving functionality?

The specific need is to be able to 'save' a data grid. Think gmail and Google Documents' autosave.

I don't want to reinvent the wheel if its already been invented. I'm looking for an existing implementation of the magical autoSave() function.

Auto-Saving:pushing to server code that saves to persistent storage, usually a DB. The server code framework is outside the scope of this question.

Note that I'm not looking for an Ajax library, but a library/framework a level higher: interacts with the form itself.

daemach introduced an implementation on top of jQuery @ http://daemach.blogspot.de/2007/03/autosave-jquery-plugin.html [script host down]. I'm not convinced it meets the lightweight and well engineered criteria though.

Criteria

  • stable, lightweight, well engineered
  • saves onChange and/or onBlur
  • saves no more frequently then a given number of milliseconds
  • handles multiple updates happening at the same time
  • doesn't save if no change has occurred since last save
  • saves to different urls per input class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

表情可笑 2024-07-29 20:36:05

我知道这个问题很旧,但我想包含一个我最喜欢的代码。 我在这里找到了它:
http://codetunnel.io/how-to-implement- autosave-in-your-web-app/

这是代码:

var $status = $('#status'),
    $commentBox = $('#commentBox'),
    timeoutId;

$commentBox.keypress(function () { // or keyup to detect backspaces
    $status.attr('class', 'pending').text('changes pending');

    // If a timer was already started, clear it.
    if (timeoutId) clearTimeout(timeoutId);

    // Set timer that will save comment when it fires.
    timeoutId = setTimeout(function () {
        // Make ajax call to save data.
        $status.attr('class', 'saved').text('changes saved');
    }, 750);
});

它会在用户停止写入超过 750 毫秒后保存。

它还具有一个状态,让用户知道更改是否已保存

I know that this question is old, but I would like to include a code that I like the most. I found it here:
http://codetunnel.io/how-to-implement-autosave-in-your-web-app/

Here is the code:

var $status = $('#status'),
    $commentBox = $('#commentBox'),
    timeoutId;

$commentBox.keypress(function () { // or keyup to detect backspaces
    $status.attr('class', 'pending').text('changes pending');

    // If a timer was already started, clear it.
    if (timeoutId) clearTimeout(timeoutId);

    // Set timer that will save comment when it fires.
    timeoutId = setTimeout(function () {
        // Make ajax call to save data.
        $status.attr('class', 'saved').text('changes saved');
    }, 750);
});

It saves after the user stops writing for more than 750 milliseconds.

It also has a status letting the user know that the changes have been saved or not

尐偏执 2024-07-29 20:36:05

自动保存的实现应该非常简单,您可以使用 jquery 或 mootools 等主要框架之一。 您需要做的就是在用户编辑应自动保存的内容后使用 window.setTimeout() ,并让超时调用 javascript 框架标准 AJAX 内容。

例如(使用 jquery):

var autosaveOn = false;
function myAutosavedTextbox_onTextChanged()
{
    if (!autosaveOn)
    {
        autosaveOn = true;

        $('#myAutosavedTextbox').everyTime("300000", function(){
             $.ajax({
                 type: "POST",
                 url: "autosavecallbackurl",
                 data: "id=1",
                 success: function(msg) {
                     $('#autosavenotify').text(msg);
                 }
             });
        }); //closing tag
    }
}

Autosave should be pretty simple to implement, and you could use one of the major frameworks like jquery or mootools. All you need to do is use window.setTimeout() once your user edits something that should be autosaved, and have that timeout call the javascript frameworks standard AJAX stuff.

For example (with jquery):

var autosaveOn = false;
function myAutosavedTextbox_onTextChanged()
{
    if (!autosaveOn)
    {
        autosaveOn = true;

        $('#myAutosavedTextbox').everyTime("300000", function(){
             $.ajax({
                 type: "POST",
                 url: "autosavecallbackurl",
                 data: "id=1",
                 success: function(msg) {
                     $('#autosavenotify').text(msg);
                 }
             });
        }); //closing tag
    }
}
风和你 2024-07-29 20:36:05

您可以通过使用超时来保存设定的时间,但是,更好的方法可能是只使用某种 onchange 事件处理程序,这样当数据更改时,如果您没有在设定的时间内保存,那么保存,但是,不要保存每次击键。

因此,在调用 ajax 函数之前,您应该查看上次保存的时间。

这将使您能够仅在需要时以预定的比率进行储蓄。 因此,如果您想每 5 分钟保存一次,那么无论进行了哪些更改,只要在您保存的 5 分钟窗口内进行了更改。

进行 ajax 调用很简单,但 jQuery 可以简化它。 不幸的是,根据我的观察,为了得到你想要的东西,你只需要实现你自己的功能。 以通用的方式很难做到,因为如果只更改某些字段,不同的人可能想要保存。 因此,仅仅因为我单击选择框可能不会导致保存功能,但更改文本框中的某些内容可能会导致保存功能。

You could save on a set time, by using timeout, but, a better method may be to just have some sort of onchange event handler, so that when data is changed, if you haven't saved within a set amount of time, then save, but, don't save on every keystroke.

So, you look to see when you last saved, before calling the ajax function.

This will enable you to save only when needed, but at a predetermined rate. So, if you want to save every 5 minutes, then regardless of what changes were made, if a change was made within that 5 minute window you save.

Making the ajax call is trivial, but jQuery can simplify it. Unfortunately, to get what you want, from what I have seen, you will need to just implement your own functionality. It is difficult to do in a general way as different people may want to save if only certain fields are changed. So, just because I click on a select box may not lead to the save function, but changing something in a text box may.

楠木可依 2024-07-29 20:36:05

For simple autosave of form fields in cookies I use this great plugin http://rikrikrik.com/jquery/autosave/
It doesn't send data to the server, but if you don't find anything better, it's easier to upgrade it's funcitonalily than do it from scratch.

溺深海 2024-07-29 20:36:05

我建议你使用 jQuery。 当然,“保存”部分仍然需要在后端完成,但是 jQuery 使 AJAX 请求的提交变得轻而易举。

如果您有 ASP.NET 后端,则可以简单地调用 WebMethod 并以指定的时间间隔提交关联的字符串(文本框的内容等):

[WebMethod]
public void AutoSave(String autoSaveContent)
{
 // Save
}

调用此方法的 jQuery 请求将是:

$.ajax({  
type: "POST",  
contentType: "application/json; charset=utf-8",  
url: "AutoSaveService.asmx/AutoSave", 
data: "{textBoxToAutosaveText}",  
dataType: "json"
});  

就这样。 您可以在 http://jquery.com/ 找到 jQuery。

I would suggest that you use jQuery. The "saving" part still has to be done on the backend, of course, but jQuery makes the submission of AJAX requests a breeze.

If you have an ASP.NET backend, you can simply call a WebMethod and submit the associated string (content of a textbox etc.) at a specified interval:

[WebMethod]
public void AutoSave(String autoSaveContent)
{
 // Save
}

The jQuery request to call this method would be:

$.ajax({  
type: "POST",  
contentType: "application/json; charset=utf-8",  
url: "AutoSaveService.asmx/AutoSave", 
data: "{textBoxToAutosaveText}",  
dataType: "json"
});  

That's all. You can find jQuery at http://jquery.com/ .

冰魂雪魄 2024-07-29 20:36:05

如果您正在寻找简单且轻量级的方法,我认为最轻量级的方法是使用 JavaScript 的内置 setTimeout() 函数。 将它与您选择的 AJAX 框架结合使用,就可以开始了。

function autoSave()
{
  $.get(URL, DATA); // Use any AJAX code here for the actual autosaving. This is lightweight jQuery.
  setTimeout("autoSave()", 60000); // Autosaves every minute.
}
autoSave(); // Initiate the auto-saving.

If you're looking for simple and lightweight, I think the most lightweight you can get is using JavaScript's built-in setTimeout() function. Use it in combination with your choice of framework for the AJAX, and you're good to go.

function autoSave()
{
  $.get(URL, DATA); // Use any AJAX code here for the actual autosaving. This is lightweight jQuery.
  setTimeout("autoSave()", 60000); // Autosaves every minute.
}
autoSave(); // Initiate the auto-saving.
脸赞 2024-07-29 20:36:05

您不需要一个每 x 秒触发一次的计时器吗? 回调函数将向表单服务器执行 AJAX 回发,并添加“autosave=true”字段。 只需在服务器上处理此回发即可完成。

Isn't all you need a timer that fires every x seconds? The callback function will do an AJAX postback to the server of the form with a "autosave=true" field added. Just handle this postback on the server and you are done.

自由如风 2024-07-29 20:36:05

synchronize 是一个jquery 插件,它向您的 html 页面添加功能,以定期自动将用户输入发送回服务器。 (源代码

JavaScript 和 HTML 示例:

<script>
  $("input").synchronize();
</script>

<input type="text" value="initial_value" 
       class="{url:'/entity.cfc?method=updateDescription',data:{ID1:'1',ID2:'2'}}" />

默认延迟后生成的 ajax 请求1 秒:

http://localhost/entity.cfc?method=updateDescription&value=update_value&preVal=initial_value&ID1=1&ID2=2

synchronize is a jquery plugin that adds functionality to your html page to periodically automatically send user input back to the server. (source code)

JavaScript and HTML sample:

<script>
  $("input").synchronize();
</script>

<input type="text" value="initial_value" 
       class="{url:'/entity.cfc?method=updateDescription',data:{ID1:'1',ID2:'2'}}" />

resulting ajax request after the default delay of 1s:

http://localhost/entity.cfc?method=updateDescription&value=update_value&preVal=initial_value&ID1=1&ID2=2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文