【发布时间】:2014-01-22 16:05:04
【问题描述】:
我有以下代码,它有一个问题,第一次加载时它不会显示任何内容,但是当我按下旋转木马上的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 = decision;
}
到
set_current_decision : function(decision) {
dificildecision.prototype.current_decision = 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/
【问题讨论】:
-
由于向原型添加构造函数会影响新实例,我认为您应该在原型函数中使用
this来引用实例,而不是函数本身的名称。例如if( this.is_animating != true ){ this.is_animating=true;// 等等 -
@czarchaic 这是最糟糕的,这样做它永远不会加载任何东西:S
-
我无法让它在小提琴上工作......也许是因为原型的东西或者我不知道(从未使用过小提琴来创建任何东西)
-
见小提琴@czarchaic jsfiddle.net/TUX5a
标签: javascript jquery html json object