Re: [問題] JavaScript 的非同步物件導向寫法
function MyOJ(param) {
this.predata = param;
this.dataA = null;
this.dataB = null;
this.processA = null;
this.processB = null;
}
MyOJ.prototype =
{'init' :
function() {
return $.when(this.getDataA(), this.getDataB());
}
,'getDataA' :
function() {
return this.processA ||
(this.processA =
$.post(urlB)
.done( function(data) { this.dataA = data; })
.done( getA_Callback )
.fail( getA_Fail )
);
}
,'getDataB' :
function() {
return this.processB ||
(this.processB =
$.post(urlC)
.done( function(data) { this.dataB = data; })
.done( getB_Callback )
.fail( getB_Fail )
);
}
}
var a = new MyOJ(params);
a.init().done(function() {
$('#dataA').text(a.dataA); //保證有資料
$('#dataB').text(a.dataB); //保證有資料
})
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.34.192.129
※ 編輯: mrbigmouth 來自: 1.34.192.129 (11/17 09:33)
推
11/17 09:45, , 1F
11/17 09:45, 1F
→
11/17 09:45, , 2F
11/17 09:45, 2F
→
11/17 11:26, , 3F
11/17 11:26, 3F
推
11/17 22:40, , 4F
11/17 22:40, 4F
→
11/17 22:41, , 5F
11/17 22:41, 5F
→
11/17 22:41, , 6F
11/17 22:41, 6F
→
11/18 08:55, , 7F
11/18 08:55, 7F
→
11/18 09:02, , 8F
11/18 09:02, 8F
→
11/18 09:02, , 9F
11/18 09:02, 9F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
Ajax 近期熱門文章
PTT數位生活區 即時熱門文章
7
20