已经添加了类型为“...”的消息的未捕获处理程序(Uncaught handler for message of type “…” already added)

系统教程 行业动态 更新时间:2024-06-14 17:04:03
已经添加了类型为“...”的消息的未捕获处理程序(Uncaught handler for message of type “…” already added)

我正在使用Shiny和JavaScript 。 我用JavaScript扩展了Shiny的功能。 这样做,我使用callbackHandlers 。 我通过在JS实现的click event观察userinput。 一旦事件被触发,我将所需的数据发送到R ,其中函数与数据一起工作。 结果再次发送回JS处理数据的显示。

所以。 在我的JS方面我有这样的事情:

for(var i = 0; i < radioButtons.length; i++) { radioButtons[i].onclick = function() { if (parseInt(this.value) === 10) { Shiny.onInputChange("go", this.value); Shiny.addCustomMessageHandler("someCallbackHandler1", function(someJson) { someFunction(someJson); }); } else if (parseInt(this.value) === 11) { Shiny.onInputChange("go", this.value); Shiny.addCustomMessageHandler("someCallbackHandler2", function(someJson) { someFunction(someJson); }); } // more else if checking for the value ....

在R方面,我这样做

observe({ if( !is.null(input$go) ) { if( (input$go == 10) ) { sendToJs1[["myJson"]] <- someFunctionIwrote(arg1, arg2, arg3, arg4) session$sendCustomMessage(type = "someCallbackHandler1", sendToJs1$myJson) } else if( (input$go == 11) ) { sendToJs2[["myJson"]] <- someFunctionIwrote(arg1, arg2, arg3, arg4, arg5, arg6) session$sendCustomMessage(type = "someCallbackHandler2", sendToJs2$myJson) } # more else if checking for which value gets send from js ....

当我点击radioButton的值为10 ,然后在radioButton上的值为11 , 然后再次在radioButton上,值为10我得到这个。

已添加类型为“someCallbackHandler1”的消息的未捕获处理程序

所以JS注意到callbackHandler1已经添加,并且不再添加它。 但我需要再次添加它才能显示数据。 此外,如果输入数据发生变化,并且someFunctionIwrote(arg1, arg2, arg3, arg4)使用不同的输入数据执行, JS做什么? 当然callbackHandler1已经添加了,但是里面有什么,从R发送到JS的结果是不同的。

我该如何处理这种情况?

I am working with Shiny and JavaScript. I extend the functionality of Shiny with JavaScript. Doing that, I work with callbackHandlers. I observe the userinput with a click event implemented in JS. Once the event is fired, I send the needed data to R, where a function works with the data. The results again get send back to JS where the display of the data handled.

So. on my JS side I have something like this:

for(var i = 0; i < radioButtons.length; i++) { radioButtons[i].onclick = function() { if (parseInt(this.value) === 10) { Shiny.onInputChange("go", this.value); Shiny.addCustomMessageHandler("someCallbackHandler1", function(someJson) { someFunction(someJson); }); } else if (parseInt(this.value) === 11) { Shiny.onInputChange("go", this.value); Shiny.addCustomMessageHandler("someCallbackHandler2", function(someJson) { someFunction(someJson); }); } // more else if checking for the value ....

On the R side I do this

observe({ if( !is.null(input$go) ) { if( (input$go == 10) ) { sendToJs1[["myJson"]] <- someFunctionIwrote(arg1, arg2, arg3, arg4) session$sendCustomMessage(type = "someCallbackHandler1", sendToJs1$myJson) } else if( (input$go == 11) ) { sendToJs2[["myJson"]] <- someFunctionIwrote(arg1, arg2, arg3, arg4, arg5, arg6) session$sendCustomMessage(type = "someCallbackHandler2", sendToJs2$myJson) } # more else if checking for which value gets send from js ....

When I click on the radioButton with the value 10, then on the radioButton with the value 11 and then again on the radioButton with the value 10 I get this.

Uncaught handler for message of type “ someCallbackHandler1” already added

So JS notices that the callbackHandler1 is already added and does not add it again. But I need it to be added again in order to display the data. Further, what would JS do if the input data changes and someFunctionIwrote(arg1, arg2, arg3, arg4) is executed with different input data? Of course the callbackHandler1 is already added, but whats inside, the results send from R to JS is different.

How would I manage this situation?

最满意答案

我的建议是:

仅在您的循环外定义您的javascript事件处理程序一次。 无需多次定义它们。 一旦创建了消息,它们将持续监听来自服务器的消息。

确保处理程序中声明的回调函数足够灵活,可以处理所有服务器响应。 所以你的函数someFunction()应该能够处理来自服务器的所有可能的消息。

所以代码是:

// function to be called from inside the callback functions: function someFunction(someJson){ console.log("No matter what someJson is, I will handle them all properly!"); ... } // set the event handlers for message from server: Shiny.addCustomMessageHandler("someCallbackHandler1", function(someJson) { someFunction(someJson); }); Shiny.addCustomMessageHandler("someCallbackHandler2", function(someJson) { someFunction(someJson); }); // set the event handlers for radio button clicks: for(var i = 0; i < radioButtons.length; i++) { radioButtons[i].onclick = function() { if (parseInt(this.value) === 10) { Shiny.onInputChange("go", this.value); } else if (parseInt(this.value) === 11) { Shiny.onInputChange("go", this.value); } // more else if checking for the value

My recommendation would be:

Define your javascript event handlers only once, and outside your loops. No need to define them more than once. They will continuously listen to the messages from the server once they have been created.

Make sure that the callback functions stated in the handlers are flexible enough to handle all your server responses. So your function someFunction() should be able to handle all possible messages from the server.

So the code would be:

// function to be called from inside the callback functions: function someFunction(someJson){ console.log("No matter what someJson is, I will handle them all properly!"); ... } // set the event handlers for message from server: Shiny.addCustomMessageHandler("someCallbackHandler1", function(someJson) { someFunction(someJson); }); Shiny.addCustomMessageHandler("someCallbackHandler2", function(someJson) { someFunction(someJson); }); // set the event handlers for radio button clicks: for(var i = 0; i < radioButtons.length; i++) { radioButtons[i].onclick = function() { if (parseInt(this.value) === 10) { Shiny.onInputChange("go", this.value); } else if (parseInt(this.value) === 11) { Shiny.onInputChange("go", this.value); } // more else if checking for the value

更多推荐

本文发布于:2023-04-24 21:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/4b7e0432a516d4ecce642a72ccb79514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:消息   类型   程序   Uncaught   added

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!