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

consider a javascript structure to house our widgets #4

Closed
timelyportfolio opened this issue Jul 18, 2014 · 7 comments
Closed

consider a javascript structure to house our widgets #4

timelyportfolio opened this issue Jul 18, 2014 · 7 comments

Comments

@timelyportfolio
Copy link
Collaborator

In the case of multiple widgets, there will be multiple payload which could be confusing and also possibly cause conflicts. In addition a widget often will return an object that allows us to do things with it beyond the initial draw/build. As an example, I have changed getScript in my fork to something like this:

getScript <- function(x, file, package){
  payload = sprintf("var payload = %s\n", RJSONIO::toJSON(x))
  # simple check for url; probably more robust method out there
  if( grep("^http",file) ) {
    script_file <- file
  } else script_file <- system.file(file, package = package)
  lines <- readLines(script_file, warn = FALSE, encoding = "UTF-8")
  tags$script(
    HTML(
      sprintf(
"
var htmlWidgets = typeof(htmlWidgets) == 'undefined' ? [] : htmlWidgets;
htmlWidgets.push(
  (function(){
    %s
    %s
  })()
)
"
, paste0(payload,collapse="n")
, paste0(lines,collapse="\n")
      )
    )
  )
}

and then a {{widget}}.js that does something like this:

return {
  "widget" : new Spinner(payload.config).spin(document.getElementById(payload.id)),
  "payload" : payload,
  "type" : "spin"
};

To test it for yourself, you could

devtools::install_github("timelyportfolio/htmlwidgets")
source('http://timelyportfolio.github.io/htmlwidgets_spin/R/spin.R')
spin()

Would love thoughts.

@ramnathv
Copy link
Owner

This is an interesting idea. Let me sit on it over the weekend to give you useful feedback.

@timelyportfolio
Copy link
Collaborator Author

a simple example in case this is easier example.html . In this application, we might want to

htmlWidgets[0].widget.stop()

once something is completed.

@ramnathv
Copy link
Owner

That makes a lot of sense. Good thinking @timelyportfolio !

@timelyportfolio
Copy link
Collaborator Author

explore this in this line

timelyportfolio referenced this issue in timelyportfolio/htmlwidgets Aug 1, 2014
@timelyportfolio
Copy link
Collaborator Author

is this something that might happen or should I abandon hope?

@ramnathv
Copy link
Owner

ramnathv commented Aug 8, 2014

@timelyportfolio This is definitely something that will have to be implemented as we make the widgets more extensible with javascript. However, I believe this is something we should do after the widget API has stabilized and we have tested it across several widgets.

@ramnathv
Copy link
Owner

ramnathv commented Aug 8, 2014

Closing this as I have opened a new issue #19 with a more concise description of the objectives. Thanks for bringing this up.

@ramnathv ramnathv closed this as completed Aug 8, 2014
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