Last active
October 17, 2019 18:34
-
-
Save neocris/9a9b1aaa012d415207a14f7b646fa2ff to your computer and use it in GitHub Desktop.
[YUI] yui sync load of sorts #load #dependency_injection #require.js #yui #modules
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
app.ports.baseurl.subscribe(function(src) { | |
$.get(src).done(function(d){ //beware of cors | |
document.querySelector("base").href = src; //check whether remote doc base meta tag is setted otherwise | |
var srcs = nosrcs = [], $htm = $(d).appendTo('<div>').parent(),i=0; | |
$htm.find('script').each(function(i,s){ | |
if($(s).is('[src]')){ | |
srcs.push((new URL($(s).attr('src'),'http://www.cm-tvedras.pt')).href); | |
} | |
else { | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.textContent = s.innerText; | |
$htm.find('*').remove(this); | |
// re-insert the script tag so it executes. | |
nosrcs.push(script); //or eval(s.innerText)? | |
srcs.push(''); | |
} | |
}); | |
$.each(nosrcs, function(){ | |
//$htm.append(this); | |
}); | |
//$('div#cmtv').append($htm); | |
//$.when($('div#cmtv').append($htm)).done(function(){console.log('done')}); | |
Y.Get.js(srcs, function (err) { | |
if (err) { | |
Y.Array.each(err, function (error) { | |
Y.log('Error loading JS: ' + error.error, 'error'); | |
$htm.append(nosrcs[i++]); | |
}); | |
//return; | |
} | |
//universal event handler delegator for all loaded docs | |
//apparently must be placed here as it seems manual triggering load event below breaks the events register | |
$('div.containerr > .content').on('click', 'a', function(e){ | |
e.preventDefault(); | |
var d = e.target; | |
d = d.nodeName == "A" ? d : $(d).parents('a:first').get(0); | |
var htm = $(d).html(); | |
var hh = $.param(htm); | |
var l = {uri: d.href,title: d.title,descriptor: "", html: htm,hash: hh,img: "", toggle: "active", height: "0px"}; | |
app.ports.clicks.send(JSON.stringify(l)); | |
}); | |
Y.log('All JS files loaded successfully!'); | |
$htm.find('*').remove('script[src]'); | |
$.when($('div.containerr > .content').append($htm)).done(function(){ | |
// Listen for the event. | |
window.addEventListener('load', function (e) { console.log('loaded'); app.ports.ackload.send(false); }, false); | |
// Dispatch the event. | |
window.dispatchEvent(new Event('load')); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment