Last active
July 12, 2016 06:32
-
-
Save ryan5500/7831b4a40f068f701a2e60e6ad43ab25 to your computer and use it in GitHub Desktop.
サードパーティJavaScriptのウィジェットの土台
This file contains 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
var Stork = Stork || {}; | |
Stork.$ = Stork.jQuery = jQuery.noConflict(true); // noconflictを使うことで外のバージョンと切り分けてjQueryをロードできる |
This file contains 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
function loadScript(url, callback) { | |
var script = document.createElement('script'); | |
script.async = true; | |
script.src = url; | |
var entry = document.getElementByTagName('script')[0]; | |
entry.parentNode.insertBefore(script, entry); | |
script.onload = script.onreadystatechange = function() { | |
// onreadystatechangeはスクリプトの読み込みの状態が変化したときに毎回発生するイベント | |
// 実際の状態はreadStateプロパティとして取得可能。 | |
// script要素の生存期間を通じていくつかの値のうち1つを取る。 | |
var rdyState = script.readyState; | |
// completeまたはloadedが読み込み完了を意味する。 | |
if (!rdyState || /complete|loaded/.test(script.readyState)) { | |
callback(); | |
script.onload = null; | |
script.onreadystatechange = null; | |
// IEにおけるメモリリークを防ぐため、イベントハンドラはデタッチする | |
} | |
}; | |
} |
This file contains 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
var Stork = (function(window, undefined) { | |
var Stork = {}; | |
function loadSupportingFiles(callback) {} | |
function getWidgetParams() {} | |
function getRatingData(params, callback) {} | |
function drawWidget() {} | |
/* ... */ | |
loadSupportingFiles(function() { | |
var params = getWidgetParams(); | |
getRatingData(params, function() { | |
/* ... */ | |
drawWidget(); | |
}); | |
}); | |
})(window); | |
// undefinedが書き換えられてる可能性を考慮し、第2引数を設定しないことで本当のundefinedへのエイリアスを貼る | |
// windowを渡す理由は、エイリアスをスコープ内で定義しないとminifierが変数名を短くできなくなるため |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment