从jQuery的AJAX请求返回的数据创建自定义JavaScript对象 [英] Creating custom JavaScript object from data returned by jQuery AJAX request

查看:77
本文介绍了从jQuery的AJAX请求返回的数据创建自定义JavaScript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建的JavaScript对象,它包含一个jQuery AJAX请求返回的数据自定义的,但我不知道这是正确的方式来做到这一点。我在想也许有办法可以使对象这样的创建,包括在构造函数中的AJAX请求:

I want to create a custom javascript object which contains data returned from a jQuery AJAX request, but I don't know which is the right way to do it. I was thinking maybe one way could be to include the AJAX request inside the constructor function so the object is created like this:

// Constructor function
function CustomObject(dataUrl) {
    var that = this;

    $.ajax(dataUrl, {
        success: function (json) {
            that.data = $.parseJSON(json);
        }
    });
}

// Creating new custom object
var myObject = new CustomObject('http://.....');

的另一种方式可以是使用一个功能,它执行AJAX和然后返回新的对象基于来自AJAX响应的数据。

Another way may be to use a function which does the AJAX and then returns the new object based on the data from the AJAX response.

function customObject(dataUrl) {
    // Constructor function
    function CustomObject(data) {
        this.data = data;
    }

    $.ajax(dataUrl, {
        success: function (json) {
            var data = $.parseJSON(json);
            return new CustomObject(data);
        }
    });
}

// Creating new custom object
var myObject = customObject('http://.....')

我想知道什么是最好的做法时做这样的事情,以及对不同方法的优点/ disadvatages。也许你可以点我对类似的东西了一些文章或例子,我想做的事情。

I would like to know what is the best practice when doing something like this, as well as advantages/disadvatages of different methods. Maybe you can point me to some article or example on something similiar to what I am trying to do.

在此先感谢。

推荐答案

我想这会是一个更好的办法,它使你的 CustomObject 只有懂行的有关数据包含。在这里,您委派创建对象工厂的工作,并通过在回调得到一个引用创建的对象,因为AJAX是异步的。如果你不介意使之同步,那么 createCustomObject 函数可以只返回实例,回调可以被删除。

I think this would be a better approach, it makes your CustomObject only knowledgeable about the data it contains. Here you delegate the work of creating objects to a factory, and pass in a callback to get a reference to the created object, since ajax is asynchronous. If you don't mind making it synchronous, then the createCustomObject function can just return the instance, and the callback can be removed.

function CustomObject(data) {
    this.data = data;
}

var factory = (function(){
   function create(dataUrl, objectClass, callback){
       $.ajax({
           url: dataUrl,
           success: function (data) {
              callback(new objectClass(data));
            }
           });
   }
   return{
      createCustomObject: function(dataUrl, callback){
          create(dataUrl, CustomObject, callback);
      }
   };
})();

// Creating new custom object
var myObject = null;
factory.createCustomObject('http://..', function(customObject){
   myObject = customObject;
});

这篇关于从jQuery的AJAX请求返回的数据创建自定义JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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