在用户控件链接JavaScript库 [英] Linking JavaScript Libraries in User Controls

查看:140
本文介绍了在用户控件链接JavaScript库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用ASP.NET MVC半年左右,已被检查出这些家伙微软创建的书呆子晚餐例子。有一件事我注意到他们确实使AJAX来RSVP了吃饭的时候,放正在使用的用户控件中的JavaScript参考RSVPing。

(FILE:RSVPStatus.ascx)

I have been using ASP.NET MVC for six months or so and have been checking out the Nerd Dinner example created by those Microsoft guys. One thing I noticed they did when enabling AJAX to RSVP for a dinner, is put the JavaScript references in the User Control being used for RSVPing.
(FILE: RSVPStatus.ascx)

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<NerdDinner.Models.Dinner>" %>

<script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>

这似乎不是我的权利,因为有我会利用这些相同的库在其他地方,如登录身份验证一个非常好的机会。另外,如果我改变脚本版本,我要追捕到库中的所有引用。

This doesn't seem right to me, as there is a really good chance I would be using these same libraries elsewhere, like logon authentication. Plus if I change script versions, I need to hunt down all the references to the libraries.

于是我问,如果我的想法是正确的,这些提法实际上应该是在一个更中心的位置就像母版页?

So I ask if my thinking is correct and these references should actually be in a more central location like the master page?

请让我知道,最好的做法是什么,这和亲的和缺点(如有)。

Please let me know what the best practice is for this and pro's and cons if any.

推荐答案

我肯定会建议不要把他们里面的谐音正好为你提到的原因。有一个高的机会,一个视图可以用两个谐音,这两个具有相同的js文件引用拉。你还要加载JS的性能命中加载HTML的其余部分之前。

I would definitely advise against putting them inside partials for exactly the reason you mention. There is a high chance that one view could pull in two partials that both have references to the same js file. You've also got the performance hit of loading js before loading the rest of the html.

我不知道最好的做法,但我选择包括母版内的任何常见的js文件,然后定义一个单独的ContentPlaceHolder为特定于视图的特定或少数一些额外的js文件。

I don't know about best practice but I choose to include any common js files inside the masterpage and then define a separate ContentPlaceHolder for some additional js files that are specific to a particular or small number of views.

下面是一个例子母版页 - 这是pretty自我解释

Here's an example master page - it's pretty self explanatory.

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<head runat="server">
    ... BLAH ...
    <asp:ContentPlaceHolder ID="AdditionalHead" runat="server" />
    ... BLAH ...
    <%= Html.CSSBlock("/styles/site.css") %>
    <%= Html.CSSBlock("/styles/ie6.css", 6) %>
    <%= Html.CSSBlock("/styles/ie7.css", 7) %>
    <asp:ContentPlaceHolder ID="AdditionalCSS" runat="server" />
</head>
<body>
    ... BLAH ...
    <%= Html.JSBlock("/scripts/jquery-1.3.2.js", "/scripts/jquery-1.3.2.min.js") %>
    <%= Html.JSBlock("/scripts/global.js", "/scripts/global.min.js") %>
    <asp:ContentPlaceHolder ID="AdditionalJS" runat="server" />
</body>

&Html.CSSBlock放大器; Html.JSBlock显然是我自己的扩展,但再次,他们是自我解释在他们做什么。

Html.CSSBlock & Html.JSBlock are obviously my own extensions but again, they are self explanatory in what they do.

然后在说一个SignUp.aspx认为我会

Then in say a SignUp.aspx view I would have

<asp:Content ID="signUpContent" ContentPlaceHolderID="AdditionalJS" runat="server">
    <%= Html.JSBlock("/scripts/pages/account.signup.js", "/scripts/pages/account.signup.min.js") %>
</asp:Content>

HTHS,
查尔斯

HTHs, Charles

诗。我将与安德鲁同意说是直接定义的母版页内的任何共同JS应串联和精缩。

Ps. I would agree with Andrew in saying that any common JS that is defined directly inside the master page should be concatenated and minified.

编辑:我的执行.JSBlock的(A,B)的要求

My implementation of .JSBlock(a, b) as requested

public static MvcHtmlString JSBlock(this HtmlHelper html, string fileName)
{
    return html.JSBlock(fileName, string.Empty);
}

public static MvcHtmlString JSBlock(this HtmlHelper html, string fileName, string releaseFileName)
{
    if (string.IsNullOrEmpty(fileName))
        throw new ArgumentNullException("fileName");

    string jsTag = string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>",
                                 html.MEDebugReleaseString(fileName, releaseFileName));

    return MvcHtmlString.Create(jsTag);
}

然后当奇迹发生......

And then where the magic happens...

    public static MvcHtmlString MEDebugReleaseString(this HtmlHelper html, string debugString, string releaseString)
    {
        string toReturn = debugString;
#if DEBUG
#else
        if (!string.IsNullOrEmpty(releaseString))
            toReturn = releaseString;
#endif
        return MvcHtmlString.Create(toReturn);
    }

这篇关于在用户控件链接JavaScript库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆