相反,我会在您的模块上公开一个方法来设置保存方法 URL,然后调用该方法,而不是设置全局变量。
<script type="text/javascript">
window.onModuleLoaded = function(module)
{
module.setSaveMethodURL('<?php echo $this->getUrl("page/save") ?>');
}
</script>
然后,在您的模块代码中,您将进行类似于以下的修改:(取决于您用于公开模块的设计模式)
module = (function ($) {
var url = saveMethodUrl;
var setSaveMethodURL = function(save_url)
{
url = save_url;
return url;
}
var returnObject = {
setSaveMethodURL: setSaveMethodURL
};
//this is executed when the module is loaded via the <script> tag)
//check to see if the moduleLoaded callback is defined
if (typeof window.onModuleLoaded != undefined)
{
//if the moduleLoaded callback is defined and is a function, call it
if (typeof window.onModuleLoaded == 'function')
{
window.onModuleLoaded(returnObject);
}
//if it's defined and is an object, iterate through the object and call
//each function in the object. (this allows you to add multiple callbacks
//to be executed when this module is loaded
else if (typeof window.onModuleLoaded == 'object')
{
for (key in window.onModuleLoaded)
{
if (typeof window.onModuleLoaded[ key ] == 'function')
{
window.onModuleLoaded[ key ](returnObject);
}
}
}
}
//return a reference to your setSaveMethodURL api method
return returnObject;
})();
至于异步加载您的模块,您可以查看有关异步加载 javascript 的其他堆栈溢出问题