JQuery的 - 存储Ajax响应为全局变量 [英] JQuery - Storing ajax response into global variable

查看:127
本文介绍了JQuery的 - 存储Ajax响应为全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在一定程度上jQuery和ajax的场景一个新手,但我有一个$就请求进行GET操作去获取一些XML文件(〜6KB或更少),但在这段时间里,用户花费在该网页上XML内容不应该/不会改变(这种设计我无法改变,我也没有权限更改XML文件,因为我从别的地方看它)。因此,我有我的响应数据存储到一个全局变量,并且对数据的任何随后的外表窗口被这个可变这样做的多个请求并不需要进行。

鉴于XML文件可以增加,我不确定这是最好的做法,也从一个Java背景的我的想法对全球公共变量一般是一个没有没有。

所以这个问题我已经是是否有可能是一个更好的方式来做到这一点,以及是否这会导致内存问题如果文件扩展出一些可笑的文件大小的问题?

我的数字数据可以被传递到XML对象内部的一些的getter / setter类型的功能,这将解决我全球公共变量问题,但还是引起了我是否应该存储对象本身内部响应的问题。

例如,我目前要做的是:

  //将code顶部
VAR XML;
 

...

  //获取文件
$阿贾克斯({
  键入:GET,
  网址:的test.xml
  数据类型:XML,
  成功:功能(数据){
                XML =数据;
            }
});
 

...

  //在后一阶段做一些与XML对象
无功富= $(XML).find('东西')ATTR('somethingElse')。
 

解决方案

有没有办法解决它,除非储存。内存分页应减少潜在的问题存在。

我会建议,而不是使用一个名为XML的全局变量,做更多的东西是这样的:

  VAR数据存储=(函数(){
    VAR XML;

    $阿贾克斯({
      键入:GET,
      网址:的test.xml
      数据类型:XML,
      成功:功能(数据){
                    XML =数据;
                }
    });

    返回{的getXML:函数()
    {
        如果(XML)返回XML;
        //别的显示一些错误,它是没有加载;
    }};
})();
 

然后访问:

  $(dataStore.getXml())找到('东西')ATTR('somethingElse')。
 

Im still somewhat of a newbie on jQuery and the ajax scene, but I have an $.ajax request performing a GET to retrieve some XML files (~6KB or less), however for the duration the user spends on that page that XML content should not / will not change (this design I cannot change, I also don't have access to change the XML file as I am reading it from somewhere else). Therefore I have a global variable that I store the response data into, and any subsequent look ups on the data are done on this variable so multiple requests don't need to be made.

Given the fact that the XML file can increase, Im not sure this is the best practice, and also coming from a java background my thoughts on global public variables are generally a no-no.

So the question I have is whether there might be a better way to do this, and a question on whether this causes any memory issues if the file expands out to some ridiculous file size?

I figure the data could be passed into a some getter/setter type functions inside the xml object, which would solve my global public variable problems, but still raises the question on whether I should store the response inside the object itself.

For example, what I currently do is:

//top of code
var xml;

...

//get the file
$.ajax({
  type: "GET",
  url: "test.xml",
  dataType: "xml",
  success : function(data) {
                xml = data;
            }
});

...

//at a later stage do something with the 'xml' object
var foo = $(xml).find('something').attr('somethingElse');

解决方案

There's no way around it except to store it. Memory paging should reduce potential issues there.

I would suggest instead of using a global variable called 'xml', do something more like this:

var dataStore = (function(){
    var xml;

    $.ajax({
      type: "GET",
      url: "test.xml",
      dataType: "xml",
      success : function(data) {
                    xml = data;
                }
    });

    return {getXml : function()
    {
        if (xml) return xml;
        // else show some error that it isn't loaded yet;
    }};
})();

then access it with:

$(dataStore.getXml()).find('something').attr('somethingElse');

这篇关于JQuery的 - 存储Ajax响应为全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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