2012-04-04 29 views
5

Tôi đang sử dụng jQuery để lưu các giá trị của các đối tượng javascript của tôi. Tôi cần truy xuất ID của đối tượng được chèn từ cơ sở dữ liệu. Tôi biết làm thế nào để làm điều đó, nếu chức năng Save nằm trong đối tượng javascript (xem mã bên dưới). Nhưng làm thế nào tôi có thể thiết lập biến ID, nếu chức năng Save không có trong đối tượng javascript?Lưu các đối tượng javascript bằng cách sử dụng jquery và chuyển một ID từ cơ sở dữ liệu

làm việc:

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 
      } 
     }); 
    }; 
}¨ 

Vì vậy, làm thế nào tôi sẽ hiện triển khai chức năng (bên ngoài lớp Person!) Như:

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. 
     } 
    }); 
}; 
+0

p trong ví dụ thứ hai của bạn không phải là một đối tượng toàn cầu, do đó bạn sẽ cần phải thiết lập cái gì khác nếu bạn muốn giữ lại mà giá trị ID bằng cách nào đó. –

+0

Thậm chí bạn có thử mã của mình không? Mã của bạn thực sự nên hoạt động. Hãy ghi nhớ có một số vấn đề với sau đó là không đồng bộ như được chỉ bởi trickyzter. Hãy thử thêm vào các cuộc gọi ajax của bạn 'async: false' để xem nó hoạt động. – Prusse

+0

Vài câu hỏi: - Bạn sẽ gọi chức năng Lưu ở đâu? - Làm cách nào bạn chuyển tham chiếu của 'p' sang hàm Lưu? – Taher

Trả lời

1

Chỉ cần làm rõ, bạn muốn thuộc tính Id đối tượng Person được cập nhật với lần lưu gần đây? Nếu vậy kịch bản sau sẽ đủ. Tôi đã sử dụng hoãn lại để đảm bảo rằng p.ID chỉ được cập nhật khi hoàn thành yêu cầu không đồng bộ.

$.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; 
}); 
0

Có thể có những cách tốt hơn để làm điều này nhưng tôi muốn có cơ sở dữ liệu của tôi cũng trả về Tên và Họ cùng với ID, sau đó tìm kiếm danh sách Person của tôi để tìm đối tượng chính xác trong hàm thành công.

0

Có lẽ đây là những gì bạn mong muốn?

tôi mượn một số mã ở đây:

/*** 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); 
Các vấn đề liên quan