优雅传递一个"单击事件"通过多种回调 [英] Elegantly passing an "click event" through multiple callbacks
问题描述
在一个给定的按钮,一个非logined用户点击,我想停止的情况下,收集他的OAuth,收集他的电子邮件,如果我没有它,然后执行该事件。
我想要做的一切在JavaScript,因为这将让事情变得更简单。
这是怎么了执行它,我有2个问题:
- 是否有这样做多个级别回调的更优雅的方式?
- 我正在触发在最后事件的方式似乎的hackish。什么可能是一个更好的办法做到这一点?
的jQuery(a.callbacktesting)。点击(函数(事件){ 如果(成功== FALSE){
。事件preventDefault();
event.stopImmediatePropagation();
验证({ENDEVENT:事件后续:afterEmail},collectEmail,failFn);
}
}); // 1级功能
功能验证(参数,可以successFn,failFn){
如果(成功=真){
successFn(参数,可以params.followup,failFn);
}其他{
failFn();
}
} //第二级功能
功能collectEmail(参数,可以successFn,failFn){
的console.log(收集电子邮件);
如果(成功=真){
successFn(PARAMS);
}其他{
failFn();
};
} //一切都完成后,要执行这个
功能afterEmail(PARAMS){
杰尔= $(params.endevent.currentTarget)
行动= params.endevent.type
jele.trigger(动作);
}
块引用>解决方案什么这个是:
如果(成功=真){
您知道,是的设置全局变量成功为真,对不对?如果要检测如果变量成功等于true,使用:
如果(成功===真){
请注意3个相等的痕迹。
做你的code严格的翻译,我得到了这样的结果:
//一切都完成后,要执行这个
功能afterEmail(事件){
$(event.currentTarget).trigger(event.type);
}//第二级功能
功能collectEmail(事件){
的console.log(收集电子邮件);
如果(成功===真){
afterEmail(事件);
}其他{
failFn();
}
}// 1级功能
功能验证(事件){
如果(成功===真){
collectEmail(事件);
}其他{
failFn();
}
}jQuery的(a.callbacktesting)。点击(函数(事件){
如果(成功=== FALSE){
。事件preventDefault();
event.stopImmediatePropagation(); 验证(事件);
}
});如果你想触发DOM事件,那么你就需要使用jQuery.trigger或相似。我会为了帮助更多需要了解你的程序的详细信息。
When a non-logined user clicks on a given button, I want to stop the event, collect his oauth, collect his email if i do not have it, and then execute the event.
I want to do everything in javascript because that would keep things much more simple.
This is how I am executing it, and I have 2 questions:
- Is there a more elegant way of doing multiple level callbacks?
- The way i am triggering the event at the end seems hackish. What might be a better way to do it?
jQuery("a.callbacktesting").click(function(event){ if (success==false){ event.preventDefault(); event.stopImmediatePropagation(); authentication({endevent:event,followup:afterEmail},collectEmail, failFn); } }); //1st level function function authentication(params, successFn, failFn){ if (success=true){ successFn(params,params.followup,failFn); }else{ failFn(); } } //2nd level function function collectEmail(params, successFn, failFn){ console.log("Collecting email"); if (success=true){ successFn(params); }else{ failFn(); }; } //After everything is done, you want to execute this function afterEmail(params){ jele=$(params.endevent.currentTarget) action=params.endevent.type jele.trigger(action); }
解决方案What's with this:
if (success = true) {
?
You realize that is setting the global variable "success" to true, correct? If you want to detect if the variable "success" is equal to true, use this:
if (success === true) {
Note the 3 equal marks.
Doing a strict translation of your code, I got this result:
//After everything is done, you want to execute this function afterEmail(event) { $(event.currentTarget).trigger(event.type); } //2nd level function function collectEmail(event) { console.log("Collecting email"); if (success === true){ afterEmail(event); } else { failFn(); } } //1st level function function authentication(event) { if (success === true) { collectEmail(event); } else { failFn(); } } jQuery("a.callbacktesting").click(function (event) { if (success === false) { event.preventDefault(); event.stopImmediatePropagation(); authentication(event); } });
If you're trying to trigger DOM events, then you'll need to use jQuery.trigger or similar. I'll need more information about your program in order to help more.
这篇关于优雅传递一个"单击事件"通过多种回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!