4

I'm looking for a bit of advice please. I'm developing a reasonably large ASP.NET MVC 3 app.

There are hundreds of partials throughout the system, so that I can break down the application in to composable chunks. Many partials contains various JQuery or straight Javascript script blocks.

The problem is that each time a partial is included in a main page, the script block will obviously be included each time, which is not the desired effect, clearly.

I could put the scripts in the main page, but there are often lots of long, detailed scripts and I don't want to clutter the page if the scripts aren't needed. (Some times the partials can appear ZERO or more times).

What's the recommended approach that would allow the script block to be included IF and ONLY if a partial is included in the page AND also included it only once?

Thanks,

Simon.

4

2 回答 2

1

我有过类似的情况;一个带有许多部分的大型 MVC3 应用程序(包含自己的脚本)。我采用的方法是对对象进行命名空间。您可以命名您的部分视图脚本,然后在部分加载时进行检查以检测命名空间是否存在或是否需要创建:

if(window.AppName.Module == undefined){
   //Do initialization
   window.AppName.Module = {};
   //Call init or whatever here
}
//Else continue on...
于 2012-11-19T16:17:31.293 回答
1

您可以将此模块添加到您的部分。

(function (appname, undefined){

    // use appName object here ..

})(window.AppName = window.AppName || {});
于 2012-11-27T13:34:35.927 回答