I'm not a fan of using globals but I couldn't figure out another way of achieving what I wanted to do:
我不是使用全局变量的粉丝,但我无法找到另一种方法来实现我想要做的事情:
window.op_ajax = null;
function addsomestuff_intohtmltable() {
$.when( op_ajax ).done(function ( data ) {
//NOW we are sure that the html-table is created with a div
// thas has class indigo
//Add some content into div with class indigo
});
}
function create_htmltable() {
var op_ajax = $.ajax({
type: 'POST', //etc, data etc...
});
op_ajax.done(function(content_data) {
//Create a table with some html
//In this table we have a div with indigo
});
op_ajax.fail(function(ts) {
alert(ts.responseText);
});
}
Above code does what it has to do. It waits for a html-table to created through an ajax-call, and then adds some content into part of that created html-table inside another function.
上面的代码完成了它必须做的事情。它等待通过ajax调用创建的html表,然后将一些内容添加到另一个函数内创建的html表的一部分中。
I never know when these two functions are executed and how long time they take. I'm using window.op_ajax to use a defered object, but is there a better way to to this (without using global variables)? I'm using some other ajax-functionality as well.
我永远不知道这两个函数何时执行以及它们需要多长时间。我正在使用window.op_ajax来使用deferred对象,但有没有更好的方法(不使用全局变量)?我也在使用其他一些ajax功能。
1 个解决方案
#1
1
For what I see, you can use two way :
对于我所看到的,你可以使用两种方式:
The ajax callback :
ajax回调:
function addsomestuff_intohtmltable() {
//Add some content into div with class indigo
}
function create_htmltable() {
$.ajax({
type: 'POST', //etc, data etc...
})
.done(function(content_data) {
//Create a table with some html
addsomestuff_intohtmltable(); // << run the second function
});
op_ajax.fail(function(ts) {
alert(ts.responseText);
});
}
It's the best choice and how you should deal with this kind of problem
这是最好的选择,以及如何处理这类问题
The other one is for advanced stuff, when the callBack is not enough :
另一个用于高级内容,当callBack不够时:
You bind a listener with your function(s) on the parent element, like an updated
on the <table>
, and you run it with a trigger
您将一个侦听器与父元素上的函数绑定在一起,就像在
#1
1
For what I see, you can use two way :
对于我所看到的,你可以使用两种方式:
The ajax callback :
ajax回调:
function addsomestuff_intohtmltable() {
//Add some content into div with class indigo
}
function create_htmltable() {
$.ajax({
type: 'POST', //etc, data etc...
})
.done(function(content_data) {
//Create a table with some html
addsomestuff_intohtmltable(); // << run the second function
});
op_ajax.fail(function(ts) {
alert(ts.responseText);
});
}
It's the best choice and how you should deal with this kind of problem
这是最好的选择,以及如何处理这类问题
The other one is for advanced stuff, when the callBack is not enough :
另一个用于高级内容,当callBack不够时:
You bind a listener with your function(s) on the parent element, like an updated
on the <table>
, and you run it with a trigger
您将一个侦听器与父元素上的函数绑定在一起,就像在