mirror of
https://github.com/moodle/moodle.git
synced 2025-07-23 23:31:58 +02:00
Instead of injecting the HTML + JS directly - return a promise that is resolve with the HTML and JS. This gives the caller control over how and when to inject the nodes in the DOM. Also modify templates.replaceNode functions to cleanup YUI events mess. Also pass the context to the callback so it can be verified. Also pass the arguments to the callback as a named array - not a flat argument list. Also - only load external scripts if they are not already loaded.
1 line
985 B
JavaScript
1 line
985 B
JavaScript
define(["jquery","core/ajax"],function(a,b){var c=function(c,d,e,f){var g=[];for(var h in f)g.push({name:h,value:f[h]});var i=a.Deferred(),j=b.call([{methodname:"core_get_fragment",args:{component:c,callback:d,contextid:e,args:g}}],!1);return j[0].done(function(a){i.resolve(a)}).fail(function(a){i.reject(a)}),i.promise()};return{loadFragment:function(b,d,e,f){var g=a.Deferred();return a.when(c(b,d,e,f)).then(function(b){var c=a(b.javascript),d="";c.each(function(b,c){c=a(c);var e=c.prop("tagName");if(e&&"script"==e.toLowerCase())if(c.attr("src")){var f=!1;a("script").each(function(b,d){return a(d).attr("src")==c.attr("src")&&(f=!0),!f}),f||(d+=" { ",d+=' node = document.createElement("script"); ',d+=' node.type = "text/javascript"; ',d+=' node.src = decodeURI("'+encodeURI(c.attr("src"))+'"); ',d+=' document.getElementsByTagName("head")[0].appendChild(node); ',d+=" } ")}else d+=" "+c.text()}.bind(this)),g.resolve(b.html,d)}).fail(function(a){g.reject(a)}),g.promise()}}}); |