2012-04-04 7 views
5

私のjavascriptオブジェクトの値を保存するためにjQueryを使用しています。挿入されたオブジェクトのIDをデータベースから取得する必要があります。 Save関数がjavascriptオブジェクト内にある場合、私はそれを行う方法を知っています(下記のコードを参照)。しかし、Save関数がjavascriptオブジェクトにない場合、ID変数をどのように設定できますか?jqueryを使用して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. 
     } 
    }); 
}; 
+0

pは2番目の例では、グローバルオブジェクトではありません、ので、あなたが何らかの形でそのIDの値を保持したい場合は、何か他のものを設定する必要があります。 –

+0

コードを試しましたか?あなたのコードは実際に動作するはずです。 trickyzterが指すような非同期的な問題がいくつかあることに留意してください。あなたのajaxにaddを試してみると、 'async:false'が呼び出されて動作しています。 – Prusse

+0

ごくわずかな質問: - 保存機能とはどこから呼び出しますか? - どのように 'p'の参照を保存機能に渡しますか? – Taher

答えて

1

明確にするために、PersonオブジェクトIDプロパティを最新の保存で更新しますか?もしそうなら、次のスクリプトで十分です。私は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; 
}); 
0

あり、これを行うには良い方法かもしれませんが、私は持っていると思います私のデータベースはIDと一緒にNameとSurnameを返してから、Personリストを検索してsuccess関数内の正しいオブジェクトを探します。

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); 
関連する問題