Question

I've used bower to install hyperagent, and it pulled down some dependencies, I'm just not sure how to properly initialise it now.

As far as I call tell, it doesn't support AMD loading, so I'm trying to use a shim config. I've tried a few things, looking something like this:

    <script src="{{ path('root') }}bower/requirejs/require.js"></script>
    <script>
        require.config({
            "baseUrl": "{{ url('root') }}/bower/",
            paths: {
                "vue": 'vue/dist/vue.min',
                "hyperagent": 'hyperagent/dist/hyperagent',
                "jquery": "jquery/jquery.min",
                "uri": "uri.js/src/URI.min"
            },
            shim: {
                'hyperagent': {
                    'deps': ['jquery', 'uri'],
                    'exports': 'Hyperagent'
                }
            }
        });

    </script>

When I later do

require(['vue', 'hyperagent'], function(Vue, Hyperagent) { ... });

Hyperagent is undefined.

Am I way off the mark? (Oh, and the mustaches are twig, this is a Symfony project)

Était-ce utile?

La solution

Thanks to Ben Weiner for this one. Taken from here.

I installed hyperagent and URIjs via bower and for now I'm just setting window.URI as a global before requiring hyperagent. Here's the relevant part of my require.js config:

require.config({
  paths: {
    'hyperagent': '../bower_components/hyperagent/dist/amd/hyperagent',
    'URIjs': '../bower_components/uri.js/src',
  }
});

To use it I just define an amd module that returns a configured hyperagent eg configured_hyperagent.js:

define(function(require) {
  window.URI = require('URIjs/URI');
  window.URITemplate = require('URIjs/URITemplate');
  Hyperagent = require('hyperagent');

  // Hyperagent.configure() etc..

  return Hyperagent;
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top