Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with Polymer and requirejs

Tags:

polymer

In an attempt to create polymer elements that use requirejs modules I ran into a blocking issue. I understand that polymer is not designed to work with requirejs, but for the time being It is my only option.

Searching for answers I found two solutions:

  • Don't use requirejs and make your modules compatible with HTML imports.
  • Put Polymer() call inside the requirejs callback as described here

Since I have to use require, at least for the time being, I went with the solution no.2. However, it turns out the solution causes asynchronous delays of element registration and incorrect data binding prior to Polymer upgrading the element.

Digging deeper into this issue, I started hacking undocumented Polymer internals with an intention to stop Polymer entirely until requirejs does its thing. Here is what I came up with:

Polymer.require = function(tag, deps, func) {
    var stopper = {}
    Polymer.queue.wait(stopper);
    require(deps, function() {
        delete stopper.__queue;
        Polymer.queue.check();
        Polymer(tag, func.apply(this, arguments));
    });
};

I know this is terribly wrong. Is there a better solution?

like image 906
Aki Avatar asked Sep 24 '14 21:09

Aki


1 Answers

I found that if I embed the call to require within the Polymer script I avoid this issue.

<link rel="import" href="../polymer/polymer.html"/>
<script src="../requirejs/require.js"></script>
<script src="../something/something.js"></script>

<polymer-element name="some-component">
    <template>...</template>
    <script>
     (function() {
       Polymer('some-component', {
            someMethod: function () {

               require(['something'], function (Something) {
                    var something = new Something();
                    ...
               }
        }
     )();
    </script>
</polymer-element>
like image 59
Shaun Netherby Avatar answered Oct 19 '22 11:10

Shaun Netherby