Skip to content

Instantly share code, notes, and snippets.

@Nitive
Last active July 28, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nitive/b9b92643b832606bca72a7ab0e0ae2d3 to your computer and use it in GitHub Desktop.
Save Nitive/b9b92643b832606bca72a7ab0e0ae2d3 to your computer and use it in GitHub Desktop.
Асинхронная подгрузка Яндекс Карт
// <script async src="https://...?lang=ru_RU&load=&onload=_onYandexMapLoaded"></script>
// or use module aload instead of async attribute (github.com/pazguille/aload)
(function () {
var ymapFunctionsStack = [];
var ymapsLoaded = false;
// random postfix to get uniq function name
window._onYandexMapLoaded = function () {
ymapsLoaded = true;
while (ymapFunctionsStack.length) {
ymapFunctionsStack.pop()();
}
};
window.ymapsRequire = function (dependencies, callback) {
if (ymapsLoaded) {
ymaps.modules.require(dependencies, callback);
} else {
ymapFunctionsStack.push(function () {
ymaps.modules.require(dependencies, callback);
});
}
};
}());
// Usage example:
// ymapsRequire(['Map', 'Placemark'], (Map, Placemark) => {
// console.log('loaded', Map, Placemark)
// })
@Nitive
Copy link
Author

Nitive commented Jul 6, 2016

For common.js

// <script async src="https://...?lang=ru_RU&load=&onload=_onYandexMapLoaded"></script>
// or use module aload instead of async attribute (github.com/pazguille/aload)

const ymapsFunctionsStack = [];

// random postfix to get uniq function name
window._onYandexMapLoaded = function _onYandexMapLoaded() {
  while (ymapsFunctionsStack.length) {
    ymapsFunctionsStack.pop()();
  }
};

module.exports = function ymapsRequire(dependencies, callback) {
  if (window.ymaps) {
    ymaps.modules.require(dependencies, callback);
  } else {
    ymapsFunctionsStack.push(() => {
      ymaps.modules.require(dependencies, callback);
    });
  }
};


// Usage example:
// ymapsRequire(['Map', 'Placemark'], (Map, Placemark) => {
//   console.log('loaded', Map, Placemark)
// })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment