第一次未加载Javascript数据

第一次未加载Javascript数据,javascript,jquery,html,json,object,Javascript,Jquery,Html,Json,Object,我有以下代码,它有一个问题,在第一次加载时它不会显示任何内容,但当我按下旋转木马上的next按钮时,一切都会正常工作,因此问题出现在第一次调用的某个地方,因为其他调用完全相同,但它们都在工作:S 我不会把显示代码放在哪里,因为它正在工作,因为它只是jquery向div和其他东西添加数据,但正如我第一次说的,数据是空的 var dificildecision = function() { } dificildecision.prototype = { is_animating : fal

我有以下代码,它有一个问题,在第一次加载时它不会显示任何内容,但当我按下旋转木马上的
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);
});
使用此代码,控制台上不会显示任何内容,但如果我更改

设置当前决策:函数(决策){
当前决定=决定;
}

设置当前决策:函数(决策){
DeficilDecision.prototype.current_decision=决策;
}

它仅在处理旋转木马功能时输出对象,但在页面加载时不输出对象

我也试着改变

get\u new\u decision:函数(decision\u id){
if(typeof(decision_id)==“undefined”| | decision_id==null){
决定_id=1;
}
$.getJSON('/~robhunter/deficildecision/web/app_dev.php/new/'
+决策标识,此。设置当前决策);
},

get\u new\u decision:函数(decision\u id){
if(typeof(decision_id)==“undefined”| | decision_id==null){
决定_id=1;
}
$.getJSON('/~robhunter/deficildecision/web/app_dev.php/new/'
+decision\u id,deficildescision.prototype.set\u current\u decision);
},

但正如原始代码一样,没有显示任何内容:S

您可以在这里试用

试试:

$(document).ready(function() {

  //  window.dificildecision = new dificildecision();
      var dificildecision = new dificildecision();
}
并在您的
deficildecision.prototype.next\u decision中使用
this

说明:

当您使用以下行声明函数时:
function dificildecision()
在全局范围内,将有一个名为
dificildecision
的新属性分配给您的窗口对象。 调用
window.deficildecision=new-deficildecision()时,此属性(对函数的引用)将替换为实例

=>这将导致应用程序中出现不可预测的行为,应避免

OP创建小提琴后更新:

我已经检查了您的小提琴,您对ajax的异步特性有问题。调用
decision=this.get_new_decision()时,您向服务器发出ajax请求以获得决策。下一行调用
decision=this.get_current_decision(),尚未设置决策(回调集\当前\决策尚未运行)

此问题有两种解决方案:

  • 通过使用
    $使用同步ajax。ajax
    函数与
    async:false
    配合使用。但是,不建议使用此解决方案,因为它会阻塞浏览器并降低用户体验
  • 使用回调或承诺API(推荐)。下面是如何执行此操作的演示:

从getJSON返回承诺

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 : 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.
    },
用于在数据准备就绪时执行回调,如果参数不是承诺,将立即调用回调(将其视为已解析的承诺):


这段代码只是一个演示如何使用promise API,您可能需要重新构造代码,使其适合此编程模型。

事实上,我发现我需要更改
get\u new\u decision
方法以使用
$.ajax
而不是
$.getJSON
,并设置
async:false
以这种方式存储变量

get_new_decision : function(decision_id) {
        if (typeof (decision_id) === "undefined" || decision_id === null) {
            decision_id = 1;
        }
        $.ajax({
            async : false,
            type : 'GET',
            url : '/~robhunter/dificildecision/web/app_dev.php/new/'
                    + decision_id,
            dataType : 'json',
            // data: myData,
            success : function(data) {

                dificildecision.prototype.set_current_decision(data);
            }
        });
    },
并改为
set\u current\u decision
方法使用
this.current\u decision=decision:)

有了这个变化,一切都完美地工作了

由于向原型添加构造函数会影响新实例,因此我认为应该在原型函数中使用
this
来引用实例,而不是函数本身的名称。e、 g.
如果(this.is\u animating!=true){this.is\u animating=true;
//etc@czarchaic这甚至是最糟糕的,因为它从不加载任何东西:SI无法让它在小提琴上工作……可能是因为原型的东西,或者我不知道(从未使用小提琴来创建任何东西)参见fiddle@czarchic@user1381537:您是否也更改为
var-dificildecision=new-dificildecision();
?是的,我也这样做过,而且我也做过。不管这个。不管什么,当做这个的时候,即使它起作用了,它也不起作用before@user1381537:检查是否正确更换
。在
DeficilDecision.prototype中更换
后。下一个决定
。您还需要更换
Deficild决策。设置当前决策
定义决策。原型。设置当前决策
在$GetJ中表示,没有任何效果,请参阅我的更新,我已更改代码以更好地工作,我还发现
设置当前决策
method@user1381537当前位置我检查了你的小提琴,你的小提琴有问题ajax的同步特性。当您调用
decision=this.get_new_decision();
时,您向服务器发出ajax请求以获取决策。下一行调用
decision=this.get_current_decision();
,决策尚未设置(回调
set_current_decision
尚未运行)此解决方案有效,但不推荐使用。请使用promise API的解决方案检查我的答案
get_new_decision : function(decision_id) {
        if (typeof (decision_id) === "undefined" || decision_id === null) {
            decision_id = 1;
        }
        $.ajax({
            async : false,
            type : 'GET',
            url : '/~robhunter/dificildecision/web/app_dev.php/new/'
                    + decision_id,
            dataType : 'json',
            // data: myData,
            success : function(data) {

                dificildecision.prototype.set_current_decision(data);
            }
        });
    },