是否确定将在JavaScript的局部视图 [英] Is it OK to put JavaScript in Partial Views

查看:91
本文介绍了是否确定将在JavaScript的局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的web应用程序的工作,其中主要的页面包含两部分:常数块始终是可见的,由3部分景色之一组成的信息块。每个的局部视图显示为AJAX请求的结果,并且被装载只一次(即切换窗口由jquery的提供后)。它运作良好,但我面对的一个问题。

的部分景色的HTML的code包含了中恒块和信息块用以及js的功能。当页面加载时,这些功能可以看到对方,它的工作原理,但ReSharper的不能找到函数的声明和警告我​​这件事。我不能将它们转移到可以在其code中找到,因为剃刀语法的外部js文件解决的问题。

我能做些什么这个?

感谢。

更新:

最后我决定从解决分离的意见我的js code中的问题。因此,新的问题是如何将剃刀语法到js文件或什么是可接受的替代。在流行的解决方案,我发现使用全局变量,数据属性和一个我更喜欢 - RazorJS库由约翰·Katsiotis

<一个href=\"http://djsolid.net/blog/razorjs---write-razor-inside-your-javascript-files\">http://djsolid.net/blog/razorjs---write-razor-inside-your-javascript-files

我希望这将稳定工作,让ReSharper的快乐。

干杯!

更新:

3年后我回忆起了这个问题,并决定根据我的经验来更新它。其实现在我宁愿建议不要使用额外的库。特别是如果你是不是在项目组的唯一成员......这是,如果你在所有的库确保好得多,它们被创造者和社会的支持,并可以很容易地集成到IDE(如果使用例如特殊的语法) 。也都从你的球队的球员应该知道它是如何工作的。所以现在我会建议做下一个事情:


  1. 保留单独的文件中所有的JS。隔离它,就像你可以。
    为其提供外部API。

  2. 从您的浏览调用API函数。

  3. 通过所有的剃刀生成的URL,短信,常量资源参数。

例如:

js文件:

  $。api.someInitFunction =功能(资源){...}

查看:

 &LT;脚本&GT;
    $ .api.someInitFunction({
        网址:{myAction:@ Url.Action(MyAction,myController的)},
        消息:{错误:@errorMessage},
        consts:{MYCONST:@myIntConst}
    });
&LT; / SCRIPT&GT;


解决方案

如果ReSharper的警告您这不是什么大不了的^ _ ^

但是,如果我是你,我不会把JavaScript中的局部视图在所有即可。

由于局部视图可以在一个页面中多次被插入,那么你会得到一个问题,你的JavaScript。

和你的情况下,如果你不能在JavaScript来JS文件分开然后就创建另一个PartialView,并把这些脚本它,只是呈现在您的主页。

I’m working on the web app where the main page contains two parts: the constant block which is always visible and the info-block made up by one of 3 partial views. Each of the partial views appears as a result of AJAX request and is loaded just once (after that switching windows is provided by jquery). It works well but I’ve faced with one problem.

The html-code of partial views contains js functions that are used in the constant block and in the info-block as well. When the page is loaded, these functions can "see" each other and it works but resharper can’t find function declarations and warn me about this. I can’t solve the problem by transferring them into external js file because of razor syntax which can be found in their code.

What can I do with this?

Thanks.

Update:

Finally I’ve decided to solve the problem separating my js code from views. So the new question was how to include razor syntax into js files or what is the acceptable alternative. The popular solutions I’ve found are using global variables, data attributes and the one I like more – RazorJS library by John Katsiotis.

http://djsolid.net/blog/razorjs---write-razor-inside-your-javascript-files

I hope it’s going to work stable and make Resharper happy.

Cheers!

Update:

After 3 years I recalled this question and decided to update it according to my experience. In fact now I would rather not recommend using additional libraries for that. Especially if you are not the only member in the project team… It is much better if you are ensured in all of your libraries, they are supported by creator and community and can be easily integrated into your IDE (if use special syntax for example). Also all the guys from your team should be aware of how does it work. So now I would suggest doing the next things:

  1. Hold all the JS in separate files. Isolate it as much as you can. Provide the external API for it.
  2. Call the API functions from your Views.
  3. Pass all the Razor generated URLs, text messages, constants as resource parameter.

For example:

js file:

$.api.someInitFunction = function(resources){ ... }

View:

<script>
    $.api.someInitFunction({
        urls: { myAction: '@Url.Action("MyAction", "MyController")' },
        messages: { error: '@errorMessage' },
        consts: { myConst: @myIntConst }
    });
</script>

解决方案

If Resharper warns you it's not a big deal ^_^

But if I were you I wouldn't put JavaScript in the partial view at all.

Because the partial view could be inserted in one page many times then you'll get an issues with your JavaScripts.

And for your case if you couldn't separate the JavaScript to JS file then just create another PartialView and put these scripts in it and just render it in your main page.

这篇关于是否确定将在JavaScript的局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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