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

Cannot load Google's jsapi inside of a component #115

Closed
pulsedemon opened this issue Apr 29, 2013 · 11 comments
Closed

Cannot load Google's jsapi inside of a component #115

pulsedemon opened this issue Apr 29, 2013 · 11 comments

Comments

@pulsedemon
Copy link

I'm trying to load this script at the top of the html file, outside of the element definition:

<script type="text/javascript" src="https://www.google.com/jsapi"></script>

which produces this error:

XMLHttpRequest cannot load https://www.google.com/jsapi?0.48633471108041704. Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin. 

These are the request headers:

GET https://www.google.com/jsapi?0.48633471108041704 HTTP/1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31
Referer: http://localhost:8000/
Origin: http://localhost:8000

Loading the library this way does work, but it causes issues with the api itself:

var jsapi = document.createElement('script');
jsapi.src = 'https://www.google.com/jsapi';
document.getElementsByTagName('head')[0].appendChild(jsapi);
@sjmiles
Copy link
Contributor

sjmiles commented Apr 29, 2013

Remember that inside of an import (target of <link rel="import"...) we have to simulate parser jobs. In particular, we must use XHR to fetch resources.

In any case 'jsapi' is particularly hard to load because it makes a lot of assumptions about where and how it's being loaded. The bottom line is that it's best to load that code directly in your application. This is how we use it in the Pica project.

<!DOCTYPE html>
<html>
<head>
  <title>Pica</title>
  <script src="https://www.google.com/jsapi"></script>
...

@pulsedemon
Copy link
Author

That would work, but then there's no way to make a self-contained component that can be dropped in anywhere if it relies on libraries like this.

Will support for this be added in the future?

@sjmiles
Copy link
Contributor

sjmiles commented Apr 29, 2013

The issue is in 'jsapi' not in toolkit.

@nickski15
Copy link

Ya. jsapi loads a lot of "crap" on the dom :)

I have it working where user has to include a JS file on the page, then all the components that depend on the library will work. I moved all my JS logic into external JS files since it's easier to compile / test.

I guess I'd like to also better understand packaging for larger projects.

@sjmiles
Copy link
Contributor

sjmiles commented Apr 30, 2013

I guess I'd like to also better understand packaging for larger projects.

This is an area we haven't concentrated on yet. You folks are helping us blaze the trail. =P

Maybe you can help identify the biggest areas of concern:

Testing? Validation of dependencies? Reuse? Reducing network transactions? Versioning? All of the above?

@ebidel
Copy link
Contributor

ebidel commented Apr 30, 2013

XD xhr "issue" is filed in HTMLImports: https://github.com/toolkitchen/HTMLImports/issues/1

@pulsedemon
Copy link
Author

Testing? Validation of dependencies? Reuse? Reducing network transactions? Versioning? All of the above?

:D that sounds about right. Personally, I'd like to see something similar to requirejs for loading javascript vs using <script>.

@dfreedm
Copy link
Member

dfreedm commented Apr 30, 2013

Technically, HTMLImports gives you a file loader that tracks dependencies. Unfortunately, something needs to bootstrap that into existence first :(.

@nickski15
Copy link

I think code reuse is important. In my app I have different logic being called from multiple components, so I have JS in it's own file. Also today I can compile that, which is nice.

I also have a bunch of html components that I now have to distribute, users have to serve, and include on every page. It would be nice if users could have a single file that included all the dependencies external JS + HTML for a set of components.

@sjmiles
Copy link
Contributor

sjmiles commented May 1, 2013

(resubmitting this comment as the last one got deformatted by goblins)

Web components already provide a lot of these facilities for you.

As Daniel points out, the de-duping mechanism allows you to import a dependency in N files without worrying about duplicates.

Additionally, imports can often simply be concatenated (certainly definitions can). It's easy to make a 'grunt' task, for example, that builds a single import for an entire collection.

Some minor amount of tooling will be needed to 'minify' code inside of imports (I assume this is what you mean by 'compile'), but this is not a difficult problem.

Lastly, you can use Custom Elements to provide 'module pattern'. See below how 'my-comp' uses 'my-module' by creating an instance of it, similar to how one might 'require' an interface using some AMD tool.

For example,

  // mymodule.html
  <element name="my-module">
    <script>
      var privateRegistry = {};
      this.register({
        register: function(inName, inValue) {
          privateRegistry[inName] = inValue;
        },
        byName: function(inName) {
          return privateRegistry[inName];
        }
      })
    </script>
  </element>

  // mycomp.html
  <link rel="import" href="my-module.html">
  <element name="my-comp">
    <script>
      var storage = document.createElement('my-module');
      this.register({
        itemName: ''
        work: function() {
          this.item = storage.byName(this.itemName);
          ...
        }
      })
    </script>
  </element>

Scott

@sorvell
Copy link
Contributor

sorvell commented Sep 25, 2013

Closing since this issues have been addressed via the discussion here.

@sorvell sorvell closed this as completed Sep 25, 2013
dfreedm pushed a commit that referenced this issue Sep 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants