5

我正在使用 jQuery 来保存我的 javascript 对象的值。我需要从数据库中检索插入对象的 ID。如果 Save 函数在 javascript 对象中,我知道该怎么做(参见下面的代码)。但是,如果 Save 函数不在 javascript 对象中,我该如何设置 ID 变量?

在职的:

Person = function() {
    var self = this;

    self.ID;
    self.Name;
    self.SurName;

    self.Save = function() {
        $.ajax({
            type: "POST",
            url: "Save",
            contentType: "application/json; charset=utf-8", 
            data: JSON.stringify({ Name: self.Name, SurnName: self.SurName }),
            dataType: "json",
            success: function (result) {
                var ID = result.d.ID; //this is the ID retreived from database
                self.ID = ID; //set the ID, it works, since I can reference to self
            }
        });
    };
}¨

那么我现在如何实现一个函数(在 Person 类之外!),例如:

SavePerson = function(p) {
     $.ajax({
        type: "POST",
        url: "Save",
        contentType: "application/json; charset=utf-8", 
        data: JSON.stringify({ Name: p.Name, SurnName: p.SurName }),
        dataType: "json",
        success: function (result) {
            var ID = result.d.ID; //this is the ID retreived from database
            p.ID = ID; //set the ID, it doesn't work, becouse if I call SavePerson repetedly for different objects, a p will not be a correct person.
        }
    });
};
4

3 回答 3

1

澄清一下,您希望 Person 对象 id 属性使用最近的保存进行更新吗?如果是这样,下面的脚本就足够了。我使用了 deferred 来确保 p.ID 仅在异步请求完成后更新。

$.Person = function() {
    var self = this;
    self.ID;
    self.Name;
    self.SurName;
}

$.SavePerson = function() {
var dfd = $.Deferred();
     $.ajax({
        type: "POST",
        url: "Save",
        contentType: "application/json; charset=utf-8", 
        data: JSON.stringify({ Name: p.Name, SurnName: p.SurName }),
        dataType: "json",
        success: dfd.resolve
    });
return dfd.promise();
};

var p = new $.Person();

$.SavePerson().then(function(result){
    p.ID = result.d.ID;
});
于 2012-04-04T12:22:10.747 回答
0

可能有更好的方法来做到这一点,但我会让我的数据库也返回 Name 和 Surname 以及 ID,然后搜索我的 Person 列表以在成功函数中找到正确的对象。

于 2012-04-04T12:17:54.877 回答
0

也许这就是你想要的?

我在这里借了一些代码:

/*** makeClass() ***
 * The following allows us to easily create
 * Javascript classes.  Source of this:
 * http://ejohn.org/blog/simple-class-instantiation/
 * makeClass - By John Resig (MIT Licensed)
 */

function makeClass() {
    return function(args) {
        if (this instanceof arguments.callee) {
            if (typeof this.init == "function") this.init.apply(this, args.callee ? args : arguments);
        } else return new arguments.callee(arguments);
    };
});

/* set up ajax */
$.ajaxSetup({
    async: false,
    type: "POST",
    contentType: "application/json",
    converters: {
        "json jsond": function(msg) {
            return msg.hasOwnProperty('d') ? msg.d : msg;
        }
    },
    data: '{}',
    dataType: "json",
    error: function(jqXHR, status, error) {
        alert("An error occurred on the server. Please contact support.");
    }
});

/* set up my person class */
var personClass = makeClass();
personClass.prototype.init = function() {
    this.ID = '';
    this.SurName = '';
    this.Name = '';
}

/* create my save function */
personClass.prototype.SavePerson = function(p) {
    this.Name = (typeof p === 'undefined') ? this.Name : p.Name;
    this.SurName = (typeof p === 'undefined') ? this.SurName : p.SurName;
    $.ajax({
        url: "Save",
        data: JSON.stringify({
            Name: this.Name,
            SurnName: this.SurName
        }),
        success: function(result) {
            var ID = result.ID; //ID  from database
            this.ID = ID; //set the ID, 
        }
    });
};
//create two persons
var person1 = personClass();
var person2 = personClass();

//set person1 to be fred
person1.Name = "Fred";
person1.SurName = "Flintstone";

// independent person
var p = {Name: "Barney", SurName: ""};
p.Surname = "Rubble";

//save specific and the independent (as person2)
person1.SavePerson();
person2.SavePerson(p);

//alert the ID of both
alert(person1.ID + ":" + person2.ID);
于 2012-04-04T15:16:35.770 回答