你写你的JavaScript在ASP.NET MVC视图...或者在一个单独的JavaScript文件? [英] do you write your JavaScript in a ASP.NET MVC view ... or in a separate JavaScript file?

查看:206
本文介绍了你写你的JavaScript在ASP.NET MVC视图...或者在一个单独的JavaScript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

努力提高我的编码风格我已经尝试了不同的解决方案,但我想不出什么是最好的。结果
我开始把我的JavaScript里面的观点,但我特别不喜欢这种解决方案。结果
很难与Visual Studio进行调试,而且各种污染的页面。结果
我的新潮流是把脚本的页面在一个单独的文件。结果
我现在面临的唯一问题是与code。结果
为了解决我定义的JavaScript变量,像这样的问题:

Trying to improve my coding styles I've tried different solutions but I can't figure out what is the best.
I've started putting JavaScript inside my views but I don't particularly like this solution.
It's hard to debug with Visual Studio, and it kinds of "pollutes" the page.
My new "trend" is to put the scripts for the page in a separate file.
The only problem I am facing is with the code.
To solve the problem I've defined JavaScript variables like this:

<script type="text/javascript">
    var PriceListFetchAction = '<%=Url.Action("Fetch", "PriceList")%>';
    var UploaderAction = '<%=Url.Action("UploadExcelPriceList", "PriceList")%>';
    var ModelId = '<%=Model.id%>';
    var ImportType = '<%=Model.Type%>';
    var customerCodeFetchAction = '<%=Url.Action("FetchByCustomerCode", "Customers")%>';
    var customerNameFetchAction = '<%=Url.Action("FetchByCustomerName", "Customers")%>';
    var ImportErpAction = '<%=Url.Action("ImportPriceListErp", "PriceList")%>';
    var imageCalendar = '<%=Url.Content("~/Content/Images/calendar.png")%>';
</script>  

然后我用这些变量在我的JavaScript文件。
什么是最好的性能,调试,风格上的吗?

and then I use the variables in my JavaScript file. What is the best in terms of performance, debugging, style for you?

推荐答案

我按照规则了一把:


  1. 请不要直接连接一个变量到DOM,除非绝对必要的。

  2. 在js文件尽可能放置尽可能多的信息。越少js文件,就更好了。

  3. 版本的js文件。当发布,通过扢或SquishIt
  4. 缩小和土豆泥
  5. 在JS,最小化动态服务器端的值依赖(生成的ID等)时即可。

  1. Don't attach a variable directly to the DOM unless absolutely necessary.
  2. Place as much information in js files as possible. The fewer js files, the better.
  3. Version your js files. When publishing, minify and mash via Chirpy or SquishIt
  4. In js, minimize your dependency on dynamic server-side values (generated ids, etc.) when you can.

所以,这里有一个例子:

So, here's an example:

我要添加的jQuery和jQuery元数据到我的项目:
http://plugins.jquery.com/project/metadata

I'll add jQuery and jQuery metadata to my project: http://plugins.jquery.com/project/metadata

然后,在我的主人js文件,我会用我自己的命名空间扩展jQuery的:

Then, in my master js file, I'll extend jQuery with my own namespace:

$.extend({
   fatDish : {
     url : {},
     urls : function(a) {
        $.extend($.fatDish.url, a);
     }
   }
});

我几乎所有的定制JS逻辑将住在 $的。fatDish 的命名空间。

现在,让我们说我想通过一个 MVC路线 $。fatDish 。在我的aspx页面,我会写:

Now, let's say I want to pass a MVC route to $.fatDish. In my aspx page, I'd write the following:

<script src="@Url.Content("~/path/master.js")" type="text/javascript"></script>
<script type="text/javascript">
   $.fatDish.urls({
      path1 : '@Url.Action("Index", "Home")'
   });
</script>

在一个js文件,我现在可以这样写:

In a js file, I can now write:

window.location = $.fatDish.url.path1;

第二种方法是使用jQuery元数据(我上面提到的)。在您的ASPX页面,你可以写这样的:

A second approach is to use jQuery metadata (which I mentioned above). On your aspx page, you could write something like:

<div class="faux-link {act:'@Url.Action("Index", "Home")'}">Go Somewhere</div>

然后,在你的js文件,你可以抓住的路线值,像这样:

Then, in your js file, you can grab the route value like so:

$('.faux-link').click(function() {
   var $this = $(this);
   var href = $this.metadata().act;
   window.location = href;
});

这篇关于你写你的JavaScript在ASP.NET MVC视图...或者在一个单独的JavaScript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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