我有以下代码,它有一个问题,在第一次加载时它不会显示任何内容,但是当我在轮播上按下 next
按钮时,一切都会正常运行,所以问题出在某个地方第一个电话,因为其他电话完全相同,但这些电话正在工作:S
我不会把显示代码放在哪里,因为它正在工作,因为它只是 jquery 将数据添加到 div 和东西,但正如我所说,第一次,数据是空的。
var dificildecision = function() {
}
dificildecision.prototype = {
is_animating : false,
base_url : window.base_url,
current_decision : null,
decisiones_seen : 0,
historial_decisiones : {
"decisiones" : []
},
is_previous : false,
previous_id : 1,
next_decision : function(replace) {
if (this.is_animating != true) {
this.is_animating = true;
if (this.is_previous) {
decision = this.get_decision(this.previous_id);
} else {
if (this.get_current_decision() === false)
decision_id = 1;
else
decision_id = this.current_decision.id;
this.get_new_decision(decision_id);
decision = this.get_current_decision();
$('#decision-carousel').html("This is: " + decision.key);
this.is_animating = false;
this.is_previous = false;
}
} else {
// console.log("Slow down");
}
},
get_new_decision : function(decision_id) {
if (typeof (decision_id) === "undefined" || decision_id === null) {
decision_id = 1;
}
$.getJSON('http://echo.jsontest.com/key/value', this.set_current_decision);
},
get_current_decision : function() {
return (this.current_decision) ? this.current_decision : false;
},
set_current_decision : function(decision) {
// THIS DOESN'T WORK
this.current_decision = decision;
// THIS WORK SECOND+ TIME EXECUTED
//dificildecision.prototype.current_decision = decision;
}
};
var dificil = new dificildecision();
dificil.next_decision(true);
$('#testlink').on('click', function(){
dificil.next_decision(true);
});
使用此代码,控制台上不会显示任何内容,但如果我更改
set_current_decision : function(decision) {
this.current_decision = 决定;
}
到
set_current_decision : function(decision) {
dificildecision.prototype.current_decision = 决定;
}
它仅在处理轮播功能时输出对象,而不是在页面加载时...
我也尝试过改变
get_new_decision : function(decision_id) {
if (typeof (decision_id) === "undefined"|| decision_id === null) {
decision_id = 1;
}
$.getJSON('/~robhunter/dificildecision/web/app_dev.php/new/'
+ decision_id, this.set_current_decision);
},
到
get_new_decision : function(decision_id) {
if (typeof (decision_id) === "undefined"|| decision_id === null) {
decision_id = 1;
}
$.getJSON('/~robhunter/dificildecision/web/app_dev.php/new/'
+ decision_id, dificildecision.prototype.set_current_decision);
},
但与原始代码完全一样,什么也没有显示 :S
您可以在这里试用 http://jsfiddle.net/TUX5a/
最佳答案
尝试:
$(document).ready(function() {
// window.dificildecision = new dificildecision();
var dificildecision = new dificildecision();
}
并在你的 dificildecision.prototype.next_decision
中使用 this
解释:
当您使用此行声明您的函数时:function dificildecision()
在全局范围内,将有一个名为 dificildecision
的新属性分配给您的窗口对象。
当您调用 window.dificildecision = new dificildecision();
时,此属性(对函数的引用)被替换为一个实例
=> 这会导致应用程序出现不可预测的行为,应该避免。
OP 创建 fiddle 后的更新:
我检查了你的 fiddle ,你对 ajax 的异步特性有疑问。当您调用 decision = this.get_new_decision();
时,您向服务器发出 ajax 请求以获取决定。在您调用 decision = this.get_current_decision();
的下一行,决策尚未设置(回调 set_current_decision 尚未运行)。
这个问题有两种解决方案:
- 通过使用
$.ajax
函数和async: false
来使用同步 ajax。但是,不推荐使用此解决方案,因为它会阻止浏览器并降低用户体验。 - 使用回调或 promise API(推荐)。以下是如何执行此操作的演示:
从 getJSON 返回一个 promise
get_new_decision : function(decision_id) {
if (typeof (decision_id) === "undefined" || decision_id === null) {
decision_id = 1;
}
return $.getJSON('http://echo.jsontest.com/key/value', this.set_current_decision);
}
并在 next_decision
中再次返回它:
next_decision : function(replace) {
if (this.is_animating != true) {
this.is_animating = true;
if (this.is_previous) {
decision = this.get_decision(this.previous_id);
} else {
if (this.get_current_decision() === false)
decision_id = 1;
else
decision_id = this.current_decision.id;
decision = this.get_new_decision(decision_id);
this.is_animating = false;
this.is_previous = false;
}
} else {
// console.log("Slow down");
}
return decision;//this could be a promise or a value.
},
使用$.when在数据准备好时执行回调,如果参数不是 promise,将立即调用回调(将其视为已解决的 promise):
$.when(dificil.next_decision(true)).then(function(decision){
$('#decision-carousel').html("This is: " + decision.key);
});
此代码只是一个如何使用 promise API 的演示,您可能需要重构您的代码以使其适合此编程模型。
关于Javascript 数据未在第一次加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20929862/