ASP.NET MVC部分视图(ASCX)中的Javascript代码与否?

时间:2021-03-03 03:38:12

Is there a "best practice" for placing Javascript code when you have many partial views and JS code that's specific to them?

当您有许多部分视图和特定于它们的JS代码时,是否存在用于放置Javascript代码的“最佳实践”?

I feel like I'm creating a maintenance nightmare by having many partial views and then a bunch of independent Javascript files for them which need to be synced up when there is a partial view change. It appears, for maintenance purposes, better to me to put the JS code with the partial view. But then I'm violating generally accepted practices that all JS code should be at the bottom of the page and not mixed in, and also I'd end up with multiple references to the same JS file (as I'd include a reference in each ASCX for intellisense purposes).

我觉得我通过提供许多部分视图创建维护噩梦,然后为他们创建一堆独立的Javascript文件,当部分视图更改时需要同步这些文件。出于维护目的,似乎我更好地将JS代码放在局部视图中。但后来我违反了普遍接受的做法,即所有JS代码都应该位于页面底部而不是混合在一起,而且我最终会对同一个JS文件进行多次引用(因为我在其中包含了一个引用)每个ASCX用于智能感知目的)。

Does anyone have a better idea? Thank you!

有没有人有更好的主意?谢谢!

2 个解决方案

#1


1  

I don't know if there are many more established "best practises" than the ones you have already stated in your question to which you would like to conform.

我不知道是否有更多已确立的“最佳实践”,而不是您希望遵循的问题中已经说明的那些。

Lately i've adopted a Html Helper approach to common js/jQuery blocks that i need for certain complex "controls" that need a lot of ajax and UI js assistance. In my most recent case i had two of these "controls" and each one required 3 script blocks.

最近我采用了Html Helper方法来处理常见的js / jQuery块,我需要一些复杂的“控件”,需要大量的ajax和UI js帮助。在我最近的情况下,我有两个这样的“控件”,每个需要3个脚本块。

I how have this helper arrangement where i can use syntax like this:

我怎么有这个帮助器安排,我可以使用这样的语法:

    <%-- // jQuery for Patron --%>
    <% =Html.MattyJq().DivListItemSingleSelector("selectPatronItem", "div#matchingPatrons",
        "patronsPrevSelectedID","PatronID") %>
    <% =Html.MattyJq().EmptyTextInputHelper("patronSearch", "patronsFilterSendEmpty") %>
    <% =Html.MattyJq().TextChangeDynamicUpdaterSelect("patronSearch","div#matchingPatrons",
        "/patrons/getpatronsitems",500,"patronsFilterSendEmpty","patron",
        "patronsPrevSelectedID","selectPatronItem") %>

I have this "Template" folder in my MVC "/Scripts" folder where i place the script blocks and then mark all the "variables" with an escape sequence such that i can swap/regex in my variables that i had passed into the helpers (above) as params.
This way i can reuse the templates and my views are much lighter/cleaner. You'll notice common params in the helpers there - that's when, for example, i have js function names or vars that are common to more than one script block. It's a sortof way of "linking" the isolated script blocks together.
Lastly i have a common section for the helpers which enables me to "go over" the resulting js/jQuery with a parser if i choose - in my case i use the .NET YUI Compressor to minify the js in Release builds - i'm sure you could add some logic here to ensure that all the script was at the bottom of the page if you liked.

我在我的MVC“/ Scripts”文件夹中有这个“Template”文件夹,我放置脚本块,然后用转义序列标记所有“变量”,这样我就可以在我传给助手的变量中交换/正则表达式(上图)作为参数。这样我可以重复使用模板,我的视图更轻/更清洁。你会注意到辅助工具中的常见参数 - 例如,当我有多个脚本块共有的js函数名或变量时。这是将孤立的脚本块“链接”在一起的一种方式。最后我有一个帮助器的公共部分,如果我选择的话,我可以使用解析器“检查”生成的js / jQuery - 在我的情况下,我使用.NET YUI Compressor来缩小发布版本中的js - 我是确定你可以在这里添加一些逻辑,以确保所有脚本都在页面底部,如果你喜欢。

#2


0  

Use a single JS and unobtrusive javascript to layer on behavior to your html pages.

使用单个JS和不显眼的javascript将行为分层到您的html页面。

Throwing js all over the place is such a maintenance nightmare, even with firebug.

整个地方投掷js就是一场维护噩梦,即使是用萤火虫也是如此。

Remember, always program as if the person maintaining your code is a violent psychopath. ;)

请记住,总是编程,好像维护你的代码的人是一个暴力的精神病患者。 ;)

#1


1  

I don't know if there are many more established "best practises" than the ones you have already stated in your question to which you would like to conform.

我不知道是否有更多已确立的“最佳实践”,而不是您希望遵循的问题中已经说明的那些。

Lately i've adopted a Html Helper approach to common js/jQuery blocks that i need for certain complex "controls" that need a lot of ajax and UI js assistance. In my most recent case i had two of these "controls" and each one required 3 script blocks.

最近我采用了Html Helper方法来处理常见的js / jQuery块,我需要一些复杂的“控件”,需要大量的ajax和UI js帮助。在我最近的情况下,我有两个这样的“控件”,每个需要3个脚本块。

I how have this helper arrangement where i can use syntax like this:

我怎么有这个帮助器安排,我可以使用这样的语法:

    <%-- // jQuery for Patron --%>
    <% =Html.MattyJq().DivListItemSingleSelector("selectPatronItem", "div#matchingPatrons",
        "patronsPrevSelectedID","PatronID") %>
    <% =Html.MattyJq().EmptyTextInputHelper("patronSearch", "patronsFilterSendEmpty") %>
    <% =Html.MattyJq().TextChangeDynamicUpdaterSelect("patronSearch","div#matchingPatrons",
        "/patrons/getpatronsitems",500,"patronsFilterSendEmpty","patron",
        "patronsPrevSelectedID","selectPatronItem") %>

I have this "Template" folder in my MVC "/Scripts" folder where i place the script blocks and then mark all the "variables" with an escape sequence such that i can swap/regex in my variables that i had passed into the helpers (above) as params.
This way i can reuse the templates and my views are much lighter/cleaner. You'll notice common params in the helpers there - that's when, for example, i have js function names or vars that are common to more than one script block. It's a sortof way of "linking" the isolated script blocks together.
Lastly i have a common section for the helpers which enables me to "go over" the resulting js/jQuery with a parser if i choose - in my case i use the .NET YUI Compressor to minify the js in Release builds - i'm sure you could add some logic here to ensure that all the script was at the bottom of the page if you liked.

我在我的MVC“/ Scripts”文件夹中有这个“Template”文件夹,我放置脚本块,然后用转义序列标记所有“变量”,这样我就可以在我传给助手的变量中交换/正则表达式(上图)作为参数。这样我可以重复使用模板,我的视图更轻/更清洁。你会注意到辅助工具中的常见参数 - 例如,当我有多个脚本块共有的js函数名或变量时。这是将孤立的脚本块“链接”在一起的一种方式。最后我有一个帮助器的公共部分,如果我选择的话,我可以使用解析器“检查”生成的js / jQuery - 在我的情况下,我使用.NET YUI Compressor来缩小发布版本中的js - 我是确定你可以在这里添加一些逻辑,以确保所有脚本都在页面底部,如果你喜欢。

#2


0  

Use a single JS and unobtrusive javascript to layer on behavior to your html pages.

使用单个JS和不显眼的javascript将行为分层到您的html页面。

Throwing js all over the place is such a maintenance nightmare, even with firebug.

整个地方投掷js就是一场维护噩梦,即使是用萤火虫也是如此。

Remember, always program as if the person maintaining your code is a violent psychopath. ;)

请记住,总是编程,好像维护你的代码的人是一个暴力的精神病患者。 ;)