簡體   English   中英

JQuery Deferred Ajax,將結果保存在調用對象中

[英]JQuery Deferred Ajax, save result in the calling object

到目前為止,我對Javascript的整個原型概念還很陌生,到目前為止,它基本上是用於創建新對象的模板。

無論如何,我有一個對象“ Schedule”,它是“ PageObject”的子級。 這是Schedule對象的函數:

Schedule.prototype.serviceSuccessFunction = function(data, status, xqr) {
  var ld;
  ld = Schedule.__super__.serviceSuccessFunction.call(this, data, status, xqr);
  ld.done($.proxy(function(x) {
    return console.log(this);
  }, this));
  ld.done($.proxy(this.render, this));
  return ld.done($.proxy(this.modernizeAndShow, this));
};

這是PageObject函數的樣子(從其子對象調用):

PageObject.prototype.serviceSuccessFunction = function(data, status, xqr) {
  $.mobile.loading("show");
  return $.when($.mobile.loadPage("pages/" + this.url, {
    pageContainer: $("#" + this.loadOptions.loadSection)
  }));
};

如您所見,PageObject返回此移動頁面加載時的延遲對象。 然后,我在Schedule對象中具有自定義.done()函數。 這些完成的函數將根據“數據”參數的含義進行渲染。

此“數據”參數來自另一個延遲的對象(使用$ .ajax)。 當我在Schedule成功函數的第一行上出現斷點時,使用ajax調用返回的內容正確填充“數據”。

現在我的問題是我希望能夠使用數據作為參數在Schedule中調用其他函數,或者將數據保存在Schedule對象本身上。 您如何將數據對象傳遞給這些函數?

當我在return console.log(this);上斷點時return console.log(this); 行,由於$ .proxy,“ this”是Schedule對象。 但是我找不到“數據”(我認為這很有意義)。 理想情況下,我想將“數據”作為參數傳遞並執行類似的操作

ld.done($.proxy(function(data) {
  this.rData = data;
}, this));

但是上面的“數據”或“ x”只是.ajax調用的URL。

我可能會誤解了,但您不能只放置this.rData = data; 在“完成”功能之外:

Schedule.prototype.serviceSuccessFunction = function(data, status, xqr) {
  var ld;
  ld = Schedule.__super__.serviceSuccessFunction.call(this, data, status, xqr);

  this.rData = data;

  ld.done($.proxy(function(x) {
    return console.log(this.rData);
  }, this));
  ld.done($.proxy(this.render, this));
  return ld.done($.proxy(this.modernizeAndShow, this));
};

然后,可以在您的委托中使用rData。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM