显示加载屏幕,而在Rails 3中执行任务 [英] Show loading screen while performing task in Rails 3

查看:101
本文介绍了显示加载屏幕,而在Rails 3中执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的创造的行动需要一段时间来加载(因为做一个API调用,然后计算)什么是用户显示加载屏幕最好的办法,而这个任务在后台执行?

If my 'create' action takes a while to load (due to doing an API call and then a calculation) what's the best way to show the user a 'loading screen' while this task is performed in the background?

推荐答案

写一些AJAX魔法;)显示加载图像时被激活,并隐藏它当Ajax调用完成。如果你不想使用AJAX,还有,嗯,你可以看看这个:

Write some AJAX magic ;) Show the loading image when activated and hide it when the AJAX call is complete. And if you don't want to use AJAX, well, hmm, you could check out this:

<一个href="http://yehudakatz.com/2010/09/07/automatic-flushing-the-rails-3-1-plan/">http://yehudakatz.com/2010/09/07/automatic-flushing-the-rails-3-1-plan/

还有一个宝石在那里,有些做到这一点。 但我没有与任何经验。 我会去的AJAX方法=)

There's also a gem out there, that somewhat does that. But I don't have any experience with that. I would go for the AJAX method =)

编辑:

是啊,你应该调用使用AJAX在jQuery的创建操作。你可以这样做:

Yeah, you should call the create action with AJAX in jQuery. You could do:

function create() {

    $('#loading_image').show();

    $.ajax({
     type: 'POST',
     url: /* URL to your create action: e.g. '/user/create/' */,
     data: $('form').serialize(),
     success: createSuccessHandler,
     error: createErrorHandler,
     complete: hideLoadingImage
    });

}

function createSuccessHandler(data) {

    alert("User created!")

}

function createErrorHandler(data) {

    alert("It failed, ffs!")

}

function hideLoadingImage() {

    $('#loading_image').hide()

}

这篇关于显示加载屏幕,而在Rails 3中执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆