简体   繁体   English

需要从ajax调用设置全局变量

[英]Need to set global variable from ajax call

I'm receiving a value from an ajax call and I need to make those variables available for other functions. 我从ajax调用中收到一个值,我需要将这些变量用于其他函数。 I having some difficulty getting the global variable set. 我在设置全局变量时遇到了一些困难。 Based on another SO question, I thought passing the variables to another function on SUCCESS would put the values in the global scope, but this isn't working. 基于另一个SO问题,我认为将变量传递给SUCCESS上的另一个函数会将值放在全局范围内,但这不起作用。

success: function (json) {
    var xCoord = json.features[0].geometry.x;
    var yCoord = json.features[0].geometry.y;
    var xyCoord = json.features[0].geometry;
    setXY(xCoord,yCoord,xyCoord);
},

function setXY(x,y,geometry){
    var xCoord = x;
    var yCoord = y; 
    var xyCoord = geometry
}

How can I accopmlish this seemingly easy task???? 我怎样才能达到这个看似简单的任务?

Declare your variables in the parent scope and then assign the values to them in the success function. 在父作用域中声明变量,然后在success函数中为它们赋值。

var xCoord, yCoord, xyCoord;

...

success: function (json) {
    xCoord = json.features[0].geometry.x;
    yCoord = json.features[0].geometry.y;
    xyCoord = json.features[0].geometry;
},

Now all functions in the parent scope have access to those variables. 现在,父作用域中的所有函数都可以访问这些变量。

you can use window object 你可以使用window对象

window.userdata = {};

function setXY(x,y,geometry){
    window.userdata.xCoord = x;
    window.userdata.yCoord = y; 
    window.userdata.xyCoord = geometry;
}

if you like, can use a global object: 如果你愿意,可以使用全局对象:

var userdata = {};    
function setXY(x,y,geometry){
    userdata.xCoord = x;
    userdata.yCoord = y; 
    userdata.xyCoord = geometry;
}

when using the keyword var, you declare a variable in the local scope. 使用关键字var时,在本地范围内声明变量。 If you're inside a function, that variable will be accessible only from inside it. 如果你在函数内部,那么只能从里面访问该变量。

You can either declare the variable in global scope, or access the global scope via window: 您可以在全局范围内声明变量,也可以通过窗口访问全局范围:

var global1 = true;

function setXY(x,y,geometry){
    global1; //return true
    window.global1; //true also
    window.global2 = 3;
    global3 = 5; // If you use no var keyword, it also points to the global scope.
} 

You can use the window object, to set global variables: 您可以使用window对象来设置全局变量:

function setXY(x,y,geometry){
    window.xCoord = x;
    window.yCoord = y; 
    window.xyCoord = geometry
}

But this is not a terribly good practice. 但这不是一个非常好的做法。 You should be tying to namespace your application to avoid cluttering the global namespace, and potential conflicts with other libraries you may want to include in the future. 您应该将应用程序命名为命名空间,以避免混淆全局命名空间,以及与您可能希望将来包含的其他库的潜在冲突。

There are a couple of ways around this. 有几种方法可以解决这个问题。

The easiest way is to define a single object in the global namespace that can keep all of your other variables within a discrete object: 最简单的方法是在全局命名空间中定义一个可以将所有其他变量保存在离散对象中的单个对象:

var myData;
function setXY(x,y,geometry){
        myData.xCoord = x;
        myData.yCoord = y; 
        myData.xyCoord = geometry
 }

Another way is with closures . 另一种方法是closures In this way you can encapsulate blocks of code away from the global space. 通过这种方式,您可以将代码块封装在全局空间之外。

(function() {
   var myVar;
   function mySuccess(data) {
     myVar = data;
     $.ajax({ url: 'test.com', success: mySuccess});
   }
})()

In this way, all code from within the closure can access myVar , but you have not needlessly added to the global namespace. 这样,闭包内的所有代码都可以访问myVar ,但是你并没有不必要地添加到全局命名空间。

Another way would be to use an object. 另一种方法是使用一个对象。

var Ajaxer = function(){

};
Axazer.prototype.setData = function(data) {
   this.data = data;
}
Azaxer.prototype.doAjax = function() {
   var self = this;
   $.ajax({ url: 'test.com', success:function(data){self.setData(data);});
}


var myAjxer = new Ajaxer();
myAjaxer.doAjax();

This way is arguable the most useful, as it allows you maintain discrete classes, that you can pass around and interact with. 这种方式可以说是最有用的,因为它允许你维护离散的类,你可以传递和交互。

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

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