我有以下功能:
Test.prototype.showToken = function () {
$('#modal').modal('show');
$('#modal').find('#btnOk').click(function (e) {
// here I want to callback
var returnValue = '123';
});
$('#modal').find('#btnProcess').click(function (e) {
// here I want to callback cancel
var returnValue = '456';
});
},
现在我在其他功能中有这个:
$.Test.showToken();
这很好用......现在我希望在我的showToken
内有一个CallBack
所以当点击按钮发生时,我会在我的其他功能中触发回调。例如:
$.Test.showToken(function(e){
// here would like to get the trigger when the btnOK is clicked and also be able to get the returnValue.
// here would like to get the trigger when the btnProcess is clicked and also be able to get the returnValue.
});
有任何线索吗?
答案 0 :(得分:0)
您可以在函数参数中接受回调函数。
Test.prototype.showToken = function (options) {
$('#modal').modal('show');
$('#modal').find('#btnOk').click(options.okButtonCallback);
$('#modal').find('#btnProcess').click(options.processButtonCallback);
}
然后当你调用你的函数时,你可以通过回调来做特殊的事情。
$.Test.showToken({okButtonCallback: function(){
// This will run on the ok button press.
}, processButtonCallback: function(){
// This will run on the process button press.
}});
答案 1 :(得分:0)
这是你想要的吗?
Test.prototype.showToken = function (okCallback, processCallback) {
$('#modal').modal('show');
$('#modal').find('#btnOk').click(function (e) {
// here I want to callback
var returnValue= ???
okCallback(returnValue)
});
$('#modal').find('#btnProcess').click(function (e) {
// here I want to callback cancel
processCallback()
});
},
/// ...
/// Usage:
Test.showToken(function(retValFromOk){
// from OK
console.log("From ok", retValFromOk);
}, function() {
// from process
});