简体   繁体   English

如何使用Firebase在javascript中使局部变量成为全局变量?

[英]How to make a local variable a global variable in javascript with firebase?

I'd like pull a value via firebase api and make it a global variable, so that I can refer to it else where in the code 我想通过firebase api提取一个值并将其设为全局变量,以便可以在代码中的其他位置引用它

I'd like for the below code to work where I pull a value from firebase and can use it outside of the function ( http://jsfiddle.net/chrisguzman/jb4qLxtb/ ) 我希望下面的代码在从firebase中提取值并可以在函数( http://jsfiddle.net/chrisguzman/jb4qLxtb/ )之外使用的地方工作

var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    return Variable;
});

alert(Variable);

I've tried defining it with var below with no luck ( http://jsfiddle.net/chrisguzman/jb4qLxtb/1/ ) 我尝试用下面的var来定义它,但是没有运气( http://jsfiddle.net/chrisguzman/jb4qLxtb/1/

var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

var MyVariable = ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    return Variable;
});


alert(MyVariable);

I've also tried to define it as a function with no luck: http://jsfiddle.net/chrisguzman/jb4qLxtb/2/ 我也尝试将其定义为没有运气的函数: http : //jsfiddle.net/chrisguzman/jb4qLxtb/2/

var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

function myFunction() { ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    return Variable;
});};


alert(myFunction());

With asynchronous programming, you can only use the response inside the callback or in a function you call from there and pass the data to. 使用异步编程,您只能在回调内部或从那里调用的函数中使用响应,并将数据传递给该响应。 You can't stuff it into a global variable to try to work around the fact that the response is asynchronous. 您不能将其填充到全局变量中,以尝试解决响应是异步的这一事实。 You also can't return a value form an asynchronous callback and expect that value to be returned from the host function. 您也不能从异步回调中返回值,并且不能期望从主机函数返回该值。 The host function has already finished executing and the callback is called some time later. 主机函数已经完成执行,回调在一段时间后被调用。

Here's one way that would work: 这是一种可行的方法:

var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    alert(Variable);
});

To read a lot more about your options for handling an async response, you can read this answer which is about an ajax call, but the concept is the same: How do I return the response from an asynchronous call? 要阅读更多有关处理异步响应的选项的信息,您可以阅读以下有关ajax调用的答案,但是概念是相同的: 如何从异步调用返回响应? .

You should declare global vars outside of $( document ).ready(function() 您应该在$( document ).ready(function()之外声明全局变量

var MyVariable = '';
$( document ).ready(function() {
var ref = new Firebase('https://helloworldtest.firebaseIO.com/');

MyVariable = ref.on('value', function (snapshot) {
    var Variable = snapshot.child("text").val();
    return Variable;
});


alert(MyVariable);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM