简体   繁体   English

如何在Coffeescript文件中使用Razor表达式?

[英]How can I use Razor expressions within a Coffeescript file?

I would like to do something like this in CoffeeScript so that I can move all my script off into coffe files: 我想在CoffeeScript中做这样的事情,以便可以将所有脚本移到咖啡文件中:

$("#btnFinish").click ->
    $.post "@Url.Action("Submit", "Process")", (response) ->
        $("#PlaceHolderButton")
            .button()
            .text response

$("#btnHome").click -> 
    window.location.href='@Url.Action("Index","Home")'

Should I just push the url and other items I need into hidden values and query them for the later when the script runs? 我是否应该将url和我需要的其他项目推送到隐藏值中,并在脚本运行时查询它们以供以后使用?

I feel like I am missing a key concept or something here. 我觉得我在这里缺少关键概念或某些东西。

Try to avoid the need of mixing javascript with server side. 尽量避免将javascript与服务器端混合使用。 There are always better workarounds. 总会有更好的解决方法。 For example: 例如:

@Html.ActionLink("Some button", "Submit", "Process", null, new { id = "btnFinish" })

and then in your js: 然后在你的js中:

$('#btnFinish').click(function() {
    $.post(this.href, function(response) {
        ...
    });
    return false;
});

or if btnFinish is some div for which you cannot use a helper to generate an url, you could use HTML5 data-* attributes, like so: 或者,如果btnFinish是某个div,而您不能使用该div来生成一个url,则可以使用HTML5 data- *属性,如下所示:

<div id="btnFinish" data-url="@Url.Action("Submit", "Process")">Some button</div>

and then: 接着:

$('#btnFinish').click(function() {
    var url = $(this).data(url);       
    $.post(url, function(response) {
        ...
    });
    return false;
});

but if you have some clickable button which you AJAXify the first approach would be semantically better because you are directly having the url as part of the href. 但是如果您具有AJAXify的可点击按钮,则第一种方法在语义上会更好,因为您直接将网址作为href的一部分。

The same thing applies for your second example: 同样的情况适用于您的第二个示例:

$('#btnHome').click(function() {
    window.location.href = $(this).data('url');
});

So you no longer need any server side tags in your javascript files. 因此,您不再需要JavaScript文件中的任何服务器端标签。 Your js are completely static, combined, minified, gzipped, cached, served from a content delivery network and all the benefits related to this. 您的js是完全静态的,可组合的,可缩小的,可压缩的,可缓存的,可从内容交付网络提供的服务,以及与此相关的所有优点。

Well to embed Razor directly into JavaScript you would need the JavaScript to go through the Razor view engine to be evaluated before it is handed off to the browser. 要将Razor直接嵌入到JavaScript中,您需要将JavaScript传递到浏览器之前,先对Razor视图引擎进行评估。 That doesn't happen currently but it could be done. 目前还没有发生,但是可以做到。

I've worked around this with these methods: - hidden field approach - <script type="text/javascript>...</script> in the view 我已经通过以下方法解决了这个问题:-隐藏字段方法-视图中的<script type="text/javascript>...</script>

You could use that last method and make a configuration management plug-in that would take in key-values in the view and provide a method to query for them from the JavaScript outside of the view. 您可以使用最后一种方法,并制作一个配置管理插件,该插件将在视图中接收键值,并提供一种从视图外的JavaScript中查询键值的方法。

There is also RazorJS which is available on Nuget: 在Nuget上也有RazorJS:

http://nuget.org/List/Packages/RazorJS http://nuget.org/List/Packages/RazorJS

Write Razor-Style C# or VB.NET insde your Javascript Files. 编写Razor样式的C#或VB.NET安装您的Javascript文件。 Also contains an http handler for serving, when needed, those files. 还包含一个http处理程序,用于在需要时提供这些文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM