bem-contrib / hackaton

Blocks that were made on the hackathon
1 stars 1 forks source link

map: доработки #2

Open sipayRT opened 9 years ago

sipayRT commented 9 years ago

Я наконец снова добрался до кода

Что сделал:

     Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened. 
h4 commented 9 years ago

Я сейчас столкнулся с загрузкой GoogleMaps и сделал пока что так:

modules.define('i-googlemaps', ['loader_type_js', 'i-googlemaps__config', 'identify'],
    function(provide, loader, config, identify) {
        var win = this.global,
            apiLoadCallback = 'googleMapsLoadCallback__' + identify(),
            loaderUrl = config.url + apiLoadCallback;

        /* global google.maps */
        function doProvide() {
            /**
             * @exports
             * @type Function
             */
            provide(google.maps);
        }

        win[apiLoadCallback] = function() {
            doProvide();
        };

        typeof google !== 'undefined' && typeof google.maps !== 'undefined' ?
            doProvide() :
            loader(loaderUrl);
    });
redhedg commented 8 years ago

Привет, подключил по Вашему методу блок i-googlemaps, но почему-то вылетает ошибка в консоли: Uncaught TypeError: window.initializegoogleMapsLoadCallbackuniq1 is not a function Вот файл i-googlemapsconfig:

modules.define('i-googlemaps__config', function(provide) {
provide(/** @exports */{
    /**
     * URL for loading GoogleMapsApi if it does not exist
     */
    url : '//maps.googleapis.com/maps/api/js?v=3&sensor=false&callback=initialize'
});
});

Весь код блока подключения здесь: https://github.com/redhedg/i-googlemaps

Что я делаю не так? Помогите пожалуйста разобраться.

h4 commented 8 years ago

Апи Google Maps возвращате код как jsonp (ну или типа того), поэтому значением параметра callback должно быть имя нашей функции. Поэтому, собственно, мы собираем loaderUrl конкатенацией.

Попробуйте изменить i-googlemaps__config на такой:

modules.define('i-googlemaps__config', function(provide) {
    provide(/** @exports */ {
        /**
         * URL for loading GoogleMaps API if it does not exist
         */
        url: '//maps.googleapis.com/maps/api/js?v=3.exp&signed_in=false&callback='
    });
});
redhedg commented 8 years ago

Спасибо большое, все заработало!

h4 commented 8 years ago

Отлично! Хотя сейчас мне кажется, что лучше сделать так для большей очевидности:

i-googlemaps__config.js

modules.define('i-googlemaps__config', function(provide) {
    provide(/** @exports */ {
        /**
         * URL for loading GoogleMaps API if it does not exist
         */
        url: function(callbackName) {
            return '//maps.googleapis.com/maps/api/js?v=3.exp&signed_in=false&callback=' + callbackName;
        }
    });
});

i-googlemaps.js

modules.define('i-googlemaps', ['loader_type_js', 'i-googlemaps__config', 'identify'],
    function(provide, loader, config, identify) {
        var win = this.global,
            apiLoadCallback = 'googleMapsLoadCallback__' + identify(),
            loaderUrl = config.url(apiLoadCallback);

        /* global google.maps */
        function doProvide() {
            /**
             * @exports
             * @type Function
             */
            provide(google.maps);
        }

        win[apiLoadCallback] = function() {
            doProvide();
        };

        typeof google !== 'undefined' && typeof google.maps !== 'undefined' ?
            doProvide() :
            loader(loaderUrl);
    });