Enforce script execution order in `browser.coffee`.

This commit is contained in:
Kit Goncharov 2011-04-11 13:38:38 -06:00
parent 238dc3a5b6
commit 2caceda8c4
3 changed files with 94 additions and 44 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,6 @@
(function() {
var CoffeeScript, runScripts;
var CoffeeScript, create, document, runScripts;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
CoffeeScript = require('./coffee-script');
CoffeeScript.require = require;
CoffeeScript.eval = function(code, options) {
@ -12,41 +13,67 @@
options.bare = true;
return Function(CoffeeScript.compile(code, options))();
};
if (typeof window == "undefined" || window == null) {
return;
document = typeof this.document !== 'undefined' ? document : null;
create = function() {
throw new Error('`XMLHttpRequest` is not supported.');
};
if (typeof this.ActiveXObject !== 'undefined') {
create = __bind(function() {
return new this.ActiveXObject('Microsoft.XMLHTTP');
}, this);
} else if (typeof this.XMLHttpRequest !== 'undefined') {
create = __bind(function() {
return new this.XMLHttpRequest;
}, this);
}
CoffeeScript.load = function(url, options) {
CoffeeScript.load = function(url, options, callback) {
var xhr;
xhr = new (window.ActiveXObject || XMLHttpRequest)('Microsoft.XMLHTTP');
xhr = create();
xhr.open('GET', url, true);
if ('overrideMimeType' in xhr) {
xhr.overrideMimeType('text/plain');
}
xhr.onreadystatechange = function() {
var error, result;
if (xhr.readyState === 4) {
return CoffeeScript.run(xhr.responseText, options);
error = result = null;
if (xhr.status === 200) {
try {
result = CoffeeScript.run(xhr.responseText);
} catch (error) {
error = exception;
}
} else {
error = new Error("An error occurred while loading the script `" + url + "`.");
}
return typeof callback == "function" ? callback(error, result) : void 0;
}
};
return xhr.send(null);
};
runScripts = function() {
var script, _i, _len, _ref;
_ref = document.getElementsByTagName('script');
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
script = _ref[_i];
if (script.type === 'text/coffeescript') {
if (script.src) {
CoffeeScript.load(script.src);
} else {
CoffeeScript.run(script.innerHTML);
}
var execute, index, length, scripts;
scripts = document.getElementsByTagName('script');
index = 0;
length = scripts.length;
(execute = function(error) {
var script;
if (error) {
throw error;
}
}
script = scripts[index++];
if (script.type === 'text/coffeescript' && script.src) {
return CoffeeScript.load(script.src, null, execute);
} else {
CoffeeScript.run(script.innerHTML);
return execute();
}
})();
return null;
};
if (window.addEventListener) {
addEventListener('DOMContentLoaded', runScripts, false);
} else {
attachEvent('onload', runScripts);
if (typeof this.addEventListener !== 'undefined') {
this.addEventListener('DOMContentLoaded', runScripts, false);
} else if (typeof this.attachEvent !== 'undefined') {
this.attachEvent('onload', runScripts);
}
}).call(this);

View File

@ -12,32 +12,55 @@ CoffeeScript.run = (code, options = {}) ->
options.bare = on
Function(CoffeeScript.compile code, options)()
# If we're not in a browser environment, we're finished with the public API.
return unless window?
# Capture a reference to the `document` object in the browser.
document = if typeof @document isnt 'undefined' then document else null
# Load a remote script from the current domain via XHR.
CoffeeScript.load = (url, options) ->
xhr = new (window.ActiveXObject or XMLHttpRequest)('Microsoft.XMLHTTP')
xhr.open 'GET', url, true
# Creates a new `XMLHttpRequest` object in IE and W3C-compliant browsers.
create = -> throw new Error '`XMLHttpRequest` is not supported.'
if typeof @ActiveXObject isnt 'undefined'
create = => new @ActiveXObject 'Microsoft.XMLHTTP'
else if typeof @XMLHttpRequest isnt 'undefined'
create = => new @XMLHttpRequest
# Load a remote script from the current domain via XHR. The optional `callback`
# function accepts two arguments: an error object if the script could not be
# loaded or failed to run, and the result of executing the script using
# `CoffeeScript.run`.
CoffeeScript.load = (url, options, callback) ->
xhr = create()
xhr.open 'GET', url, yes
xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
xhr.onreadystatechange = ->
CoffeeScript.run xhr.responseText, options if xhr.readyState is 4
if xhr.readyState is 4
error = result = null
if xhr.status is 200
try result = CoffeeScript.run xhr.responseText catch error then error = exception
else
error = new Error "An error occurred while loading the script `#{url}`."
callback? error, result
xhr.send null
# Activate CoffeeScript in the browser by having it compile and evaluate
# all script tags with a content-type of `text/coffeescript`.
# This happens on page load.
# In the browser, the CoffeeScript compiler will asynchronously load, compile,
# and evaluate all `script` elements with a content type of
# `text/coffeescript`. The scripts are loaded and executed in order on page
# load.
runScripts = ->
for script in document.getElementsByTagName 'script'
if script.type is 'text/coffeescript'
if script.src
CoffeeScript.load script.src
else
CoffeeScript.run script.innerHTML
scripts = document.getElementsByTagName 'script'
index = 0
length = scripts.length
(execute = (error) ->
throw error if error
script = scripts[index++]
if script.type is 'text/coffeescript' and script.src
CoffeeScript.load script.src, null, execute
else
CoffeeScript.run script.innerHTML
execute()
)()
null
# Listen for window load, both in browsers and in IE.
if window.addEventListener
addEventListener 'DOMContentLoaded', runScripts, no
else
attachEvent 'onload', runScripts
# Execute scripts on page load in W3C-compliant browsers and IE.
if typeof @addEventListener isnt 'undefined'
@addEventListener 'DOMContentLoaded', runScripts, no
else if typeof @attachEvent isnt 'undefined'
@attachEvent 'onload', runScripts