使用jquery保存javascript对象并从数据库传递ID
Saving javascript objects using jquery and passing an ID from database
我正在使用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.
}
});
};
为了澄清,您希望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;
});
可能有更好的方法可以做到这一点,但我会让我的数据库也返回Name和Surname以及ID,然后搜索我的Person列表,在success函数中找到正确的对象。
也许这就是你想要的?
我在这里借用了一些代码:
/*** 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);
相关文章:
- 在不重构Mongo数据库的情况下,更新嵌入数组中的具有给定ID的对象
- 如何设置从数据库到php会话的id点击链接
- 单击图像时从数据库加载选项id
- 我想单击一个
- 并获取 id 以通过 POST 将其插入数据库,可以吗?
- 如何将下拉列表中检索到的值的id从数据库发送到php文件
- 如何在 PHP 和数据库中的 OpenTracker() 中传递动态 ID
- 数据表 - 通过单击事件获取当前行的数据库 ID
- 将 PHP 数据库中的 ID 保存为变量
- 根据按 id 搜索将数据库的值设置为文本框
- 将数据库 ID 排序到垂直时间轴中
- 从数据库中检索的选中复选框的数据库中获取ID
- 当 Javascript 中的 POST ID 元素插入 PDO 时,数据库中没有结果
- 如何通过Firebase数据库中的AngularJS在点击时获取数组元素的唯一ID
- angularjs选择框基于数据库id的默认选择
- 创建Raphael Path与数据库ID之间的关系
- 如何传递数据库ID到Rails URL路径
- 我如何得到mysql数据库id随机回声
- 如何从HTML/Javascript隐藏数据库ID
- Jquery数据表服务器端处理按数据库id选择行
- 哪里是在客户端上放置数据库ID的最佳位置