Created
November 13, 2012 02:17
-
-
Save kara-ryli/4063560 to your computer and use it in GitHub Desktop.
Loads the google IMA library exactly once.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global YUI*/ | |
/** | |
* Loads the google IMA library exactly once. | |
* | |
* https://developers.google.com/interactive-media-ads/docs/sdks/googlehtml5 | |
* | |
* @module google-ima | |
* @requires event-custom | |
*/ | |
YUI.add("google-ima", function (Y) { | |
var IMA_LOADED = "ima:loaded", | |
win = Y.config.win, | |
yLangIsFunction = Y.Lang.isFunction, | |
loadTO, | |
onLibLoaded = function (ima) { | |
Y.Global.fire(IMA_LOADED, ima); | |
}, | |
onLibSuccess = function () { | |
onLibLoaded(win.google.ima); | |
}, | |
onIMALoaded = function () { | |
clearTimeout(loadTO); | |
onLibLoaded(win.google.ima); | |
}, | |
onGoogleLoaded = function () { | |
var google = win.google; | |
if (google && google.ima) { | |
onLibSuccess(google.ima); | |
} else if (google && google.load) { | |
// if it takes longer than 10 seconds to load | |
// cancel and call it an error | |
loadTO = setTimeout(onLibLoaded, 10 * 1000); | |
google.load("ima", "1", { callback: onIMALoaded }); | |
} else { | |
onLibLoaded(); | |
} | |
}; | |
if (!Y.Global.getEvent(IMA_LOADED, true)) { | |
Y.Global.publish(IMA_LOADED, { fireOnce: true }); | |
if (win.google) { | |
onGoogleLoaded(); | |
} else { | |
Y.Get.script("http://www.google.com/jsapi", { | |
onSuccess: onGoogleLoaded, | |
onFailure: onLibLoaded | |
}); | |
} | |
} | |
/** | |
* loads the IMA library as quickly as possible, while only loading the | |
* library exactly once. | |
* @method loadGoogleIMA | |
* @static | |
* @for YUI | |
* @param {Function} cb Called when the library is loaded. Passed the google.ima object as an argument. | |
* @return {CustomEvent} An event handler for when the library is loaded. | |
**/ | |
Y.loadGoogleIMA = function (cb) { | |
return Y.Global.once(IMA_LOADED, function (g) { | |
if (yLangIsFunction(cb)) { | |
cb(g); | |
} | |
}); | |
}; | |
}, "3.5.0", { requires: ["event-custom"] }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment