将加载的json传递给多个函数 [英] Passing loaded json to multiple functions

查看:85
本文介绍了将加载的json传递给多个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有几个函数可以完成多个任务,但是我需要通过大多数JSON对象。为什么需要使用多个jsons的原因。



使用函数加载我的json ...

 函数LoadJSON(path){
$ .getJSON(path,function(){})
.done(function(results){return results;})
.fail(function(){alert (Error loading content。);});
}

...从那里我尝试将它加载到函数中...

 函数LoadScene(button){
...
LoadButtons(LoadJSON('js / content。 json'),id); // id是由代码
}

确定的......终于我尝试访问数据在我最需要它的功能...

 函数LoadButtons(obj,id){
alert( OBJ [ '数据']);

...我已经设法直接加载它,没有加载函数或传递它从一个功能到另一个功能,但是为了使我的代码更加高效,我需要正确地分离事物。然而,它不工作:我已经设法使用LoadButtons函数内的ajax调用,但是当我将它分成三个单独的函数时,它将不起作用,该警报返回未定义。



任何理由为什么这不起作用我怎么实现它?

LoadJSON 不会返回,因为它是异步的 - 所以 LoadButtons(LoadJSON())不起作用。

您需要为成功传递回调函数,例如:

 <$ c $ (函数(结果){
if($ .isFunction(ondone))函数LoadJSON(path,ondone){
$ .getJSON(path,function(){})
.done )ondone(results);
})
.fail(function(){alert(Error loading content。);});
}

函数LoadDone(json)
{
..使用json
}

// call with:

LoadJSON('js / content.json',function(results){LoadDone(results);})
//或
LoadJSON('js / content.json' ,LoadDone)

或直接使用承诺:

 函数LoadJSON(path,ondone){
return $ .getJSON(path,function(){})
.fail(function(){alert( 加载内容时出错。);});

$ b $ LoadJSON('js / content.json')
.done(function(results){
LoadDone(results)
});






我想添加一些关于 return here:

 函数LoadJSON(路径){
$。 getJSON(path,function(){})
.done(function(results){return results;})
.fail(function(){alert(Error loading content。);}) ;
}

首先,我会补充说,没有在LoadJSON中返回,所以你总是会得到空值,但是kind-of是一个返回值,它在 .done 中。但是这不是LoadJSON的一部分,因为它稍后发生(如 $。getJSON 是异步的)。

所以会发生什么是:



  • LoadJSON启动

  • 看到$ .getJSON,并且:ok - 我会开始ajax调用给出了您提供的详细信息,立即返回

  • LoadJSON退出(没有返回语句,因此返回 null

  • 然后ajax调用完成并触及 .done ,它只返回结果回到jquery ajax处理程序 >


So I have a few functions working to do multiple things however I need to pass a json object through most of them. Reasons behind why is the need to use multiple jsons.

I load my json using a function...

function LoadJSON(path) {
    $.getJSON(path, function() { })
        .done(function(results) { return results; })
        .fail(function() { alert("Error loading content."); });
}

...from there I try to load it into a function...

function LoadScene(button) {
    ...
    LoadButtons(LoadJSON('js/content.json'), id); // id is determined in code
}

...finally I try to access the data in the function I need it the most...

function LoadButtons(obj, id) {
    alert(obj['data']);
}

... I have managed to load it directly without a load function or passing it from one function to another, however to make my code more efficient I need to properly separate things.

However, it's not working: I have managed to use the ajax call inside the LoadButtons function, however when I separate it into three separate functions, it won't work and the alert returns "undefined".

Any reason why this wouldn't work how I am implementing it?

解决方案

Your LoadJSON won't ever return json because it's async - so LoadButtons(LoadJSON()) won't work.

You need to pass a callback function for the success, eg:

function LoadJSON(path, ondone) {
    $.getJSON(path, function() { })
        .done(function(results) { 
            if ($.isFunction(ondone)) ondone(results);
        })
        .fail(function() { alert("Error loading content."); });
}

function LoadDone(json)
{
    .. work with json
}

// call with:

LoadJSON('js/content.json', function(results) { LoadDone(results); })
//or
LoadJSON('js/content.json', LoadDone )

or use the promise directly:

function LoadJSON(path, ondone) {
    return $.getJSON(path, function() { })
        .fail(function() { alert("Error loading content."); });
}

LoadJSON('js/content.json')
    .done(function(results) {
        LoadDone(results)
    });


I wanted to add some detail regarding the return here:

function LoadJSON(path) {
    $.getJSON(path, function() { })
        .done(function(results) { return results; })
        .fail(function() { alert("Error loading content."); });
}

At first, I going to add that there's no return in LoadJSON so you'll always get null, but there "kind-of" is a return, it's in the .done. But this is not part of LoadJSON as it occurs later (as $.getJSON is async)

So what happens is:

  • LoadJSON starts
  • Sees $.getJSON and goes: ok - I'll start the ajax call given the details you've provided and returns immediately
  • LoadJSON exits (with no return statement, so returns null
  • The ajax call then completes and hits the .done which just returns the result back to the jquery ajax handler

这篇关于将加载的json传递给多个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆