在ajax和文档准备就绪后,Rails最好的初始化Javascript函数的方法 [英] Rails best way to initialize function in Javascript after ajax and document ready
问题描述
问题是,我需要调用某些函数来初始化页面上的某些元素.但是在ajax成功之后,我不得不在多个地方一次又一次地调用这些函数.我想知道是否使用以下组合
The question is that there are some functions that I call to initial some elements on the page. But after ajax success, I have to re-call those functions again and again in multiple places. I was wondering beside using the following combo
$(document).ready(function(){
function A
});
$(document).ajaxComplete(function(){
function A
});
我了解到可以对setTimeout进行某些操作,并增加线程计时,以延迟来自链接
I read that there are something I can do with the setTimeout and clock up the thread to delay the function call from the link http://googlecode.blogspot.com/2009/07/gmail-for-mobile-html5-series-using.html but I have a hard time digesting it. If someone can break it down for me.
更新:
我的意思是,当我在多个ajax成功内执行html更新时,我必须调用函数A来重新初始化,上面的代码是我的主意,但我认为应该有更好的方法
I meant that when I do an html updated inside multiple ajax success, I have to call function A to re initialize and the code above is my idea but I think there should be a better way
示例
$(document).on('click', 'a', function() {
$.ajax({
type: 'GET',
url: 'some url',
data: data,
success: function(data) {
$('#some-sector').html(data);
function A; <------- to init
}
});
});
$(document).on('click', 'b', function() {
$.ajax({
type: 'GET',
url: 'another url',
data: data,
success: function(data) {
$('#some-sector').html(data);
function A; <------- to init
}
});
});
另一个更新:
因此,基本上,页面上有一些元素需要通过调用函数A动态更新.从示例中,我有多个ajax来更新页面.我想知道是否有更好的方法来实现此目的,而不是多次调用A函数来实现Ajax成功.我唯一想到的就是最高代码.
So Basically there are some elements on the page that I need to update dynamically by calling function A. And from the example, I have multiple ajax that updates a page. Instead of calling function A in multiple the ajax success, I was wondering if there is a better way to do this. The only thing that i could think of it's the top code.
推荐答案
据我了解,您正在DOM上调用某些函数组 用作
As far as i understand you are calling some group of functions on DOM ready function as
$(document).ready(function(){
function_01();
function_02();
function_03();
});
并希望在ajax请求上调用相同的功能
and want to call the same function on the ajax request
$(document).ajaxComplete(function(){
function_01();
function_02();
function_03();
});
您可以定义一个通用函数,在内部调用所有需要的函数
You can define a common function that call internally all the functions that are in need
function callAll()
{
function_01();
function_02();
function_03();
}
然后按以下方式调用
$(document).ready(callAll);
$(document).ajaxComplete(callAll);
您要一次又一次地调用此 callAll 方法的逻辑..
at what logic you want to cal this callAll method again and again..
更新
尝试按如下所示调用函数
try calling the function as below
$(document).ready(function () {
function callall()
{
alert("call all");
}
$(document).ajaxComplete(callall);
});
,这将在ajax请求处理后调用初始化函数
and this will call the initialize function after the ajax request is processed
希望它会有所帮助...........
Hope it helps...........
这篇关于在ajax和文档准备就绪后,Rails最好的初始化Javascript函数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!