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

Document how to compile with requirejs optimizer when using async plugin #45

Open
idflood opened this issue Dec 19, 2013 · 9 comments
Open

Comments

@idflood
Copy link

idflood commented Dec 19, 2013

Hi,

When using the async plugin to load google maps it become impossible to build and minify the project with the requirejs optimizer.

I've found a partial solution but it is missing something to be complete:
http://techblog.realestate.com.au/loading-google-maps-with-requirejs/

@millermedeiros
Copy link
Owner

I use the async plugin on many projects. never had issues with the optimization, the async plugin should not be executed during build, see:

if(config.isBuild){
onLoad(null); //avoid errors on the optimizer
}else{

@rmariuzzo
Copy link

I have the same problem... using the optimization, the dev console say:

ReferenceError: google is not defined

@alizbazar
Copy link

Have you found any sensible resolution? Facing the same problem...

@rmariuzzo
Copy link

@alizbazar, I couldn't use the async plugin successfully after the optimizer process...

@zawilliams
Copy link

Also having the same issue after the optimizer. Trying to find a solution.

@zawilliams
Copy link

@millermedeiros maybe I'm using the async plugin totally wrong in my project, but I'm doing a JSON call to an API and pulling in the data to a template. Should I be doing something different if this isn't meant for a build?

@zawilliams
Copy link

Ok so - found my issue was using Almond. Stopped using it and it worked with the build. Kind of a bummer, but it works now.

@resoliwan
Copy link

i have same issue but is not a requirejs-plugins problem

if u use shim config

check this requirejs/r.js#623

after add option (wrapShim: true) at build.js

'ReferenceError: google is not defined' problem solved

ps.

if u not use deps property at shim config
r.js can not find dependency for shim config

'shim': {
'vendor/jq-map/jquery.ui.map': {deps:['jquery', 'wishbeen/gmap']}
}

@lagden
Copy link

lagden commented May 3, 2016

Create a simple module (lazy load)
That will work when you build using almond

Example

// lazy.js
define(function () {
    var h = document.getElementsByTagName('head')[0];
    function addScript(src, cb) {
        var s = document.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = src;
        if (cb) {
            s.onload = cb;
        }
        h.insertBefore(s, h.lastChild);
    }
    return addScript;
});

Now load google maps API

// map.js
define(['lib/lazy'], function (lazy) {
    lazy((document.location.protocol === 'https:' ? 'https' : 'http') + '://www.google.com/jsapi', function () {
        google.load('maps', 3, {other_params: 'key=your-google-key', callback: initialize});
    });

    function initialize() {
        var ll = new google.maps.LatLng(29.9752502, 31.0675272);
        var mapCanvas = document.getElementById('map-canvas');
        var mapOptions = {
            zoom: 17,
            center: ll,
            mapTypeControl: false,
            panControl: false,
            scrollwheel: false,
            zoomControl: true
        };
        var map = new google.maps.Map(mapCanvas, mapOptions);
        var marker = new google.maps.Marker({
            position: ll,
            map: map,
            title: 'Sphinx'
        });
        var infowindow = new google.maps.InfoWindow({
            content: [
                '<div>',
                '<h3>Sphinx</h3>',
                '<p>Great Sphinx of Giza</p>',
                '</div>'
            ].join('')
        });
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.open(map, marker);
        });
    }
});

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

7 participants