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

Dynamically loading a remote Cell script from an existing Cell app and injecting into the existing Cell app. #142

Open
gliechtenstein opened this issue Jul 8, 2017 · 2 comments

Comments

@gliechtenstein
Copy link
Contributor

gliechtenstein commented Jul 8, 2017

I was trying to implement an app that consists of just an input field initially, which when submitted will dynamically load a remote script and do something on onload event, using Cell.

More specifically, I was trying to find a way to load a remote cell app from an existing cell app, and inject it into the existing cell app.

But I ran into a problem. When loading a remote script dynamically, it is not possible to know what variables have loaded as a result of that script. The new manual instantiation feature introduced in #125 doesn't help because we have no knowledge about the newly introduced variables in the root context.

Anyway here's the half-finished code. The only problematic part is the Fill in the blank here part inside onload(). Once that's figured out, everything should just work.

{
  $cell: true,
  _load: function(url) {
    this.querySelector("#script_slot").$components.push({
      $type: "script",
      src: url,
      onload: function() {
        /*
         * Fill in the blank here
         * Need to turn the loaded gene objects into cells
         * but we do not know what variables have loaded.
         */
      }
  	});
  },
  $components: [
    { 
      id: "script_slot",
      $components: []
    },
    {
      $type: "input",
      placeholder: "Enter a javascript url and press enter",
      onkeyup: function(e) {
        // when the enter key is pressed, find the nearest ancestor
        // that contains _load() and call that function.
        if(e.keyCode === 13) this._load(this.value);
      }
    }
  ]
}

Trying to figure out a nice and clean way to tackle this.

it could be done through a modification of the API, or maybe there's a way to do this without modifying the API. Maybe there's a clean way.

@gliechtenstein gliechtenstein changed the title [Puzzle] How to know what variables have loaded as a result of a dynamic script load triggered by cell? [Puzzle] Dynamically loading a remote Cell script from an existing Cell app and injecting into the app. Jul 8, 2017
@gliechtenstein gliechtenstein changed the title [Puzzle] Dynamically loading a remote Cell script from an existing Cell app and injecting into the app. Dynamically loading a remote Cell script from an existing Cell app and injecting into the app. Jul 8, 2017
@gliechtenstein gliechtenstein changed the title Dynamically loading a remote Cell script from an existing Cell app and injecting into the app. Dynamically loading a remote Cell script from an existing Cell app and injecting into the existing Cell app. Jul 8, 2017
@p3k
Copy link

p3k commented Jul 22, 2017

do i understand this correctly that you are you looking for a way to determine which global variables have been added after loading a script?

if so, could some of the answers in this thread help or do the trick?

https://stackoverflow.com/questions/17276206/list-all-js-global-variables-used-by-site-not-all-defined

@gliechtenstein
Copy link
Contributor Author

Thanks for sharing!

Yup, in fact this is how Cell implements its automatic instantiation mechanism https://github.com/intercellular/cell/blob/develop/cell.js#L501

But the challenge I'm trying to point out here is that currently there's no way to detect whether a variable has already been used or not.

In the example above, we need to detect only the variables that have been dynamically loaded as a result of the script load. The Object.keys(window) approach will return all the variables, not just the newly added ones. Hope I'm making sense, it's a niche scenario but I think it can be powerful if we can figure this out.

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