Skip to content

Commit

Permalink
Add Kernel#require_remote along with opal-parser
Browse files Browse the repository at this point in the history
This is primarily to support script with src attr pointing to an rb file and also to make it easy to require stuff from all "text/ruby" scripts.
  • Loading branch information
elia committed Mar 24, 2014
1 parent b553c71 commit 0c8b4db
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions stdlib/opal-parser.rb
Expand Up @@ -8,6 +8,16 @@ def eval(str)
code = Opal.compile str
`eval(#{code})`
end

def require_remote url
source = %x{
var r = new XMLHttpRequest();
r.open("GET", url, false);
r.send('');
return r.responseText;
}
eval source
end
end

%x{
Expand All @@ -23,11 +33,13 @@ def eval(str)
};
function run_ruby_scripts() {
var tags = document.getElementsByTagName('script');
var tag, tags = document.getElementsByTagName('script');
for (var i = 0, len = tags.length; i < len; i++) {
if (tags[i].type === "text/ruby") {
Opal.eval(tags[i].innerHTML);
tag = tags[i];
if (tag.type === "text/ruby") {
if (tag.src) Opal.Kernel.$require_remote(tag.src);
if (tag.innerHTML) Opal.Kernel.$eval(tag.innerHTML);
}
}
}
Expand Down

0 comments on commit 0c8b4db

Please sign in to comment.