首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript数据第一次未加载

Javascript数据第一次未加载
EN

Stack Overflow用户
提问于 2014-01-05 03:54:43
回答 2查看 811关注 0票数 1

我有下面的代码,它有一个问题,在第一次加载时它不会显示任何东西,但是当我按下旋转木马上的next按钮时,一切都会很好地工作,所以问题就在第一个调用的某个地方,因为其他调用完全相同,但是它们是工作的:S。

我不会把显示代码放在哪里,因为它正在工作,因为它只是将数据添加到div之类的地方,但是正如我第一次说的,数据是空的。

代码语言:javascript
运行
复制
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/

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-05 05:48:54

尝试:

代码语言:javascript
运行
复制
$(document).ready(function() {

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

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

解释:

当您在全局范围中使用以下一行:function dificildecision()声明函数时,将有一个名为dificildecision的新属性分配给您的window对象。调用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 (推荐)。下面是一个演示如何做到这一点:

演示

回报getJSON的承诺

代码语言:javascript
运行
复制
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中再次返回它

代码语言:javascript
运行
复制
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执行回调,如果参数不是承诺,则会立即调用回调(将其视为已解决的承诺):

代码语言:javascript
运行
复制
$.when(dificil.next_decision(true)).then(function(decision){
        $('#decision-carousel').html("This is: " + decision.key);
});

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

票数 2
EN

Stack Overflow用户

发布于 2014-01-06 09:32:50

实际上,我发现我需要更改get_new_decision方法以使用$.ajax而不是$.getJSON,并以这种方式设置async: false来存储变量

代码语言:javascript
运行
复制
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; :)通过此更改,一切都工作得很完美!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20929862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档