Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in _getElements when using selector & ns() #175

Closed
avsdev-cw opened this issue Oct 17, 2018 · 8 comments
Closed

Bug in _getElements when using selector & ns() #175

avsdev-cw opened this issue Oct 17, 2018 · 8 comments

Comments

@avsdev-cw
Copy link

This bug occurs when trying to use the "selector" parameter of shinyjs::hide when you are also using shiny namespaces. The id parameter is always set using the namespace internally by shinyjs (even though the id parameter defaults to NULL) and due to the order of the if/else the selector parameter effectively gets ignored. I believe the precedence order of of _getElements needs to be changed using the below patch.

A better/future fix might be to check the id parameter of hide() because using NULL is a valid parameter:

library(shiny)
serv <- function(input, output, session) {
   print(session$ns(NULL)) # prints: [1] "test_ns"
}
runApp(shinyApp(fluidPage(), function(input, output){ callModule(serv, "test_ns"); }))

I'm assuming this also applies to hideElement(), show(), showElement(), toggle() and toggleElement() since they all use the _getElements method

quick patch:

diff --git a/inst/srcjs/shinyjs-default-funcs.js b/inst/srcjs/shinyjs-default-funcs.js
index 36b7f4a..c43713e 100644
--- a/inst/srcjs/shinyjs-default-funcs.js
+++ b/inst/srcjs/shinyjs-default-funcs.js
@@ -64,10 +64,10 @@ shinyjs = function() {
     var $els = null;
     if (params.elements !== null && typeof params.elements !== "undefined") {
       $els = params.elements;
-    } else if (params.id !== null && typeof params.id !== "undefined") {
-      $els = _jqid(params.id);
     } else if (params.selector !== null && typeof params.selector !== "undefined") {
       $els = $(params.selector);
+    } else if (params.id !== null && typeof params.id !== "undefined") {
+      $els = _jqid(params.id);
     }
     if ($els === null || $els === undefined || $els.length == 0) {
       shinyjs.debugMessage("shinyjs: Could not find DOM element using these parameters:");

@avsdev-cw
Copy link
Author

avsdev-cw commented Oct 17, 2018

NOTE: bug is probably a regression introduced by floriandeboissieu@5c1b450

@daattali
Copy link
Owner

The commit you mention is in a different repository - is this bug present in this shinyjs or if the forked copy?

If it's present here, please include a reproducible example

@avsdev-cw
Copy link
Author

avsdev-cw commented Oct 17, 2018

Ignore my mention on that commit, it was in a previous bug report on this repository and I didn't realise the referenced commit was on a fork.

Having looked at the source code I believe "asis" would also fix my problem in this case (pulling master now to check).

Even if it does, the order of precedence between id and selector probably still needs looking at since you could logically use a selector with an id instead of the id parameter to do the same, however:

hide("myId") !== hide(selector = paste0("#", session$ns("myId")))

but in reality, it should

@daattali
Copy link
Owner

Can you please include a full reproducible example that showcases the issue? Thanks :)

@avsdev-cw
Copy link
Author

Ah, commit 8627e85 fixes the issue in master.

Any ETA on when a new stable will be available on CRAN?

@daattali
Copy link
Owner

Before end of year

@avsdev-cw
Copy link
Author

Great, Thanks!

@daattali
Copy link
Owner

@avsdev-cw it looks like I was a bit off... by more than a year! In case you wanted to know, I just submitted the new version to CRAN so it should be on CRAN next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants