在运行时为处理程序添加/更改通道(Add/Change channel for handler at runtime)
在3.1.0电路中,有没有办法在运行时为处理程序设置通道? 一个有用的替代方法是在运行时添加处理程序并指定通道。
我检查了Manager.addHandler实现,但无法使其工作。 我试过了:
self._my_method.__func__.channel = _my_method_channel self._my_method.__func__.names = ["event name"] self.addHandler(self._my_method)
In circuits 3.1.0, is there a way to set at runtime the channel for a handler? An useful alternative would be to add a handler at runtime and specify the channel.
I've checked the Manager.addHandler implementation but couldn't make it work. I tried:
self._my_method.__func__.channel = _my_method_channel self._my_method.__func__.names = ["event name"] self.addHandler(self._my_method)
原文:https://stackoverflow.com/questions/31999671
最满意答案
首先声明一个将在按钮点击时调用的函数:
/** * Function that performs various operations on button click * @param btn {jQuery object}: The button that triggered the click event */ function nano(btn) { // Do your stuff here... btn.appendTo("div#name_selected") .removeClass("names") .addClass("added_name"); // Get the value attribute var name_value = btn.attr("value"); // Make ajax call if (name_value) { ajaxSubmit(name_value); } }
声明要执行ajax调用的函数:
function ajaxSubmit(val) { // Check if 'val' is not undefined if (val) { $.ajax({ type:"POST", url:"z.php", cache:false, data: { val: val }, success:function(data) { window.location.href="show.php"; }, error:function() { // error handling here... } }); } }
在所需元素上添加事件侦听器:
$(document).on('click', '.button-class', function() { // Call nano passing the clicked button as parameter // this --> the javascript object that triggered the click event // $(this) --> the jQuery object of 'this' nano($(this)); });
重要提示 :
从所有按钮中删除
onClick
属性! ( 不要使用它 ,而是使用on()
函数添加事件侦听器)First of all declare a function that is going to be called on button click:
/** * Function that performs various operations on button click * @param btn {jQuery object}: The button that triggered the click event */ function nano(btn) { // Do your stuff here... btn.appendTo("div#name_selected") .removeClass("names") .addClass("added_name"); // Get the value attribute var name_value = btn.attr("value"); // Make ajax call if (name_value) { ajaxSubmit(name_value); } }
Declare the function that is going to perform the ajax call:
function ajaxSubmit(val) { // Check if 'val' is not undefined if (val) { $.ajax({ type:"POST", url:"z.php", cache:false, data: { val: val }, success:function(data) { window.location.href="show.php"; }, error:function() { // error handling here... } }); } }
Add an event listener on the desired elements:
$(document).on('click', '.button-class', function() { // Call nano passing the clicked button as parameter // this --> the javascript object that triggered the click event // $(this) --> the jQuery object of 'this' nano($(this)); });
IMPORTANT:
Remove
onClick
attributes from all your buttons! (Don't use it, instead use theon()
function to add event listeners)
相关问答
更多-
不明白为什么不允许; 很久以前在N0295中提出了嵌套函数,它说: 我们讨论将嵌套函数引入到C ++中。 嵌套功能被很好地理解,他们的介绍不需要编译器供应商,程序员或委员会的努力。 [...] 显然这个提案被拒绝了,但是由于我们1993没有上网会议时间,所以我们没有理由拒绝这个拒绝。 实际上,这个建议在Lambda表达式和C ++闭包中被注明为可能的替代方案: C51委员会[SH93]的一篇文章[Bre88]和提案N0295建议将嵌套函数添加到C ++中。 嵌套函数类似于lambda表达式,但被定义为函数体 ...
-
Python中的名称在绑定之前不存在。 将def foo(...):块移动到使用foo()的代码上方。 Names in Python do not exist until they are bound. Move the def foo(...): block above the code that uses foo().
-
您可以返回name并将其传递给另一个函数,除非使用global关键字,否则函数中的变量对于它们声明的函数是局部的,但返回是一个更好的选择: def getname(): name = raw_input("What is your name?") print("Ok," + name) return name # return the name so we can use it later def getage(name): age = raw_input("What is ...
-
def question(attempt, answer): score = 0 #if attempt is true, add 1 to score #if attempt is false, do nothing to score if attempt == answer: score += 1 return score print("Your score is %r." % score) 我会在函数内打印它,它返回: >>> ...
-
首先声明一个将在按钮点击时调用的函数: /** * Function that performs various operations on button click * @param btn {jQuery object}: The button that triggered the click event */ function nano(btn) { // Do your stuff here... btn.appendTo("div#name_selected ...
-
我不一定建议这样做(没有对所有要求的更多理解),但是您遇到的问题与assign在执行时看到的范围有关。 无论它在哪里寻找,最重要的是它实际上分配给一个名为boo的变量,但不在test_fun的范围内,也不在以后容易检索的方式。 一个快速解决方案可能是做类似的事情: test_fun <- function(x) { myenv <- environment() foo <- x %>% assign("boo", ., envir = myenv) # somethi ...
-
如何根据变量值在函数内定义函数(How to define a function inside a function depending on variable values)[2021-09-16]
您想使用匿名函数。 这是一个已知问题 ( 另一个问题也显示了您的问题 )。 function f(option::Bool) if option g = () -> println("option true") else g = () -> println("option false") end g end 在v0.5中,匿名函数和泛型函数之间没有性 ... -
使用var语句时,该变量仅为本地变量: var msg = "1111"; 否则,该值将转义为全局范围。 The variable is only local when the var statement is used: var msg = "1111"; Otherwise the value escapes into the global scope.
-
你可以使用typedef typedef String OneString(String x); typedef String OneStringAndInt(String x, int y); void main() { //var f = (String x) => '$x'; var f = (String x, int y) => '$x$y'; if(f is OneString) { print(f('bla')); } else { print(f('bla' ...
-
使用bash你无法真正得到它(我想你想要在shell的几个实例之间共享一些变量......)。 但是,如果你切换到chsh (使用chsh来更改你的登录shell),你就会得到所谓的通用变量,这种变量很适合。 另请参阅相关问题的答案 。 顺便说一句,你应该阅读高级bash脚本指南并考虑使用bash函数(而不是脚本)。 如果您只想在同一个 shell进程内的多个shell函数之间共享一个变量,那么就不要将它声明为函数local ! With bash you cannot really get that (I ...