我正在为我的工作场所编写一些遗留代码,无法弄清楚如何处理返回的数据对象中的数据。 retrieveThis函数应该检索对象数据:
myObj.retrieveThis(new myObj.getThisData({num : 10, page : 1, sorting : "stampDescending"}), function () {myCallback(this);});
var myObj = function () {
var getThisData = {
// this function populates an array and returns it to the retrieveThis function
}
var theObject = {
retrieveThis: function(a, b) {
var OBJ = {};
// OBJ is populated by the data from var getThisData...I checked
setTimeout(function () {
b(OBJ);
}, 1000);
}
}
return theObject;
})();
我无法将数据(“theObject”)传递给我的回调函数(或根本没有)。我将this
传递给myCallback(),其中myCallback为:
function myCallback(obj) {
alert(Object.keys(obj));
}
警告框会显示包含document
,jQuery
和myObj
的密钥列表。看起来OBJ中的数据是从数组allTheData中填充的,但我似乎无法将其传回(如返回theObject;)来处理它。我在哪里错了?
注意 - 我无法修改此遗留代码。我需要处理“theObject”。
答案 0 :(得分:2)
您传递了错误的回调函数
在里面用参数调用它,所以你应该用参数
来定义它override func viewDidLoad() {
super.viewDidLoad()
timeLabel.text = "00:00"
if let object = currentObject {
audioTitle.text = object["audioTitle"] as? String
let days = object["daysActive"] as! Int
daysActive.text = "Powertalks: Day \(days)"
var initialThumbnail = UIImage(named: "trc_app_icon.png")
audioImage.image = initialThumbnail
if let thumbnail = object["image"] as? PFFile {
audioImage.file = thumbnail
audioImage.loadInBackground()
}
if let audioFile = object["audioFile"] as? PFFile {
if let audioPath: String = audioFile.url {
audioPlayer = AVPlayer(URL: NSURL(string: audioPath))
audioSlider.minimumValue = 0
audioSlider.maximumValue = Float(CMTimeGetSeconds(audioPlayer.currentItem.asset.duration))
audioSlider.value = Float(CMTimeGetSeconds(audioPlayer.currentTime()))
audioPlayer.volume = volumeSlider.value
playAudio()
}
}
}
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateSlider"), userInfo: nil, repeats: true )
}
在这种情况下,在myObj.retrieveThis(
new myObj.getThisData({num : 10, page : 1, sorting : "stampDescending"}),
myCallback);
传递myCallback
对象