【问题标题】:Javascript data not loading at first timeJavascript数据第一次未加载
【发布时间】: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


【解决方案1】:

试试:

$(document).ready(function() {

  //  window.dificildecision = new dificildecision();
      var dificildecision = new dificildecision();
}

并在您的dificildecision.prototype.next_decision 中使用this

说明:

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

=> 这会导致应用程序出现不可预知的行为,应该避免。

OP 创建小提琴后更新:

我检查了你的小提琴,你对 ajax 的异步特性有疑问。当您调用decision = this.get_new_decision(); 时,您会向服务器发出一个ajax 请求以获得决定。下一行你调用decision = this.get_current_decision();,决定还没有设置(回调 set_current_decision 还没有运行)。

这个问题有两种解决方案:

  • 通过使用$.ajax 函数和async: false 来使用同步ajax。但是,不建议使用此解决方案,因为它会阻止浏览器并降低用户体验。
  • 使用回调或承诺 API(推荐)。下面是一个演示如何做到这一点:

DEMO

从 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中再次返回:

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,回调会立即被调用(认为是re​​solved的promise):

$.when(dificil.next_decision(true)).then(function(decision){
        $('#decision-carousel').html("This is: " + decision.key);
});

此代码只是演示如何使用 Promise API,您可能需要重新构建代码以使其适合此编程模型。

【讨论】:

  • @user1381537:你也改成var dificildecision = new dificildecision();了吗?
  • 是的,我也这样做了,并将 dificildecision.whatever 替换为 this.whatever,这样做的时候,即使之前工作过,它也不起作用
  • @user1381537:检查您是否正确替换了this。在dificildecision.prototype.next_decision 中替换this 之后。您还需要在 $getJSON 中将 dificildecision.set_current_decision 替换为 dificildecision.prototype.set_current_decision
  • 试过了,没有用,看我的更新,我已经更改了代码以更好地工作,我还发现set_current_decision方法的异常行为
  • @user1381537:我检查了你的小提琴,你对 ajax 的异步特性有疑问。当您调用decision = this.get_new_decision(); 时,您会向服务器发出一个ajax 请求以获得决定。而下一行你调用decision = this.get_current_decision();,决定还没有设置(回调set_current_decision 还没有运行)
【解决方案2】:

实际上我发现我需要将get_new_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_current_decision 方法以使用this.current_decision = decision; :) 有了这个变化,一切都完美无缺!

【讨论】:

  • 此解决方案有效,但不推荐。通过使用 promise API 的解决方案检查我的答案
猜你喜欢
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 2019-07-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多