我对JS很陌生。我有一个辅助类,可以对服务器进行一些常见的获取。目前,其中一个功能如下所示:
getAvailablePlatforms: function (platform) {
var self = this;
if (_.isUndefined(platform) || platform == '') {
platform = "crystalproject";
}
$.ajax({
url: '/api/platform/' + platform,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
console.log("Got data");
self.platforms = data;
self.eventAggregator.trigger('getplatforms');
},
});
}
所以我想让这个更灵活的是传入回调函数。所以我尝试了这个:
getAvailablePlatforms: function (platform, callback) {
var self = this;
if (_.isUndefined(platform) || platform == '') {
platform = "crystalproject";
}
$.ajax({
url: '/api/platform/' + platform,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success: function (data) {
console.log("Got data");
self.platforms = data;
self.eventAggregator.trigger('getplatforms');
if (callback && typeof(callback) === "function") {
callback(data);
}
},
});
}
我想怎么称呼它,是这样的:
var helper = new Helper();
helper.getAvailablePlatforms('', this.populatePlatforms(???????));
我希望我的回调函数使用它在回调中收到的数据。我怎样才能做到这一点?先谢谢了!