jquery - How do you load a javascript file into a scope -
i'm trying figure out how load contents of javascript file scope. reason want able have plug , play capability , able load different functionality app. perhaps user role determines functionality loaded, or perhaps want tailor app given client.
here's example of contents of 1 of pluggable javascript file:
myapp.loginviewmodel = (function () { //data var self = this; return { helloworld: function () { alert('hellow world login view model'); } }; })(); here example of configuration file listing js files should loaded:
var myapp = myapp || {}; myapp.viewmodelregistry = { baseviewmodel: { javascriptpath: "/scripts/viewmodels/baseviewmodel.js"}, loginviewmodel:{ javascriptpath: "/scripts/viewmodels/loginviewmodel.js"} }; here's code loops through viewmodelregistry , uses jquery load script:
initializeapp: function () { //register view models (var viewmodelname in myapp.viewmodelregistry) { var registeredmodel = myapp.viewmodelregistry[viewmodelname]; $.getscript(registeredmodel.javascriptpath, function (data, textstatus, jqxhr) { }); } console.log(myapp); } }; my goal load javascript function js file myapp scope. each pluggable js file scoped myapp. thought using jquery's getscript load js file automatically update myapp scope since pluggable js files designed scope. however, console.log(myapp) doesn't reveal code js files. missing? feasible?
i believe console.log executed before of js files have been retrieved , parsed. other that, seem on correct course. feasible, , terminology used describe want achieve called asynchronous module definitions.
Comments
Post a Comment