问题描述
我正在开发 Web 应用程序,其中主页包含两部分:始终可见的常量块和由 3 个局部视图之一组成的信息块.每个部分视图都作为 AJAX 请求的结果出现并且只加载一次(之后切换窗口由 jquery 提供).它运行良好,但我遇到了一个问题.
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.
部分视图的 html 代码包含在常量块和信息块中使用的 js 函数.当页面加载时,这些函数可以看到"彼此并且可以工作,但是 resharper 找不到函数声明并警告我这一点.由于可以在他们的代码中找到 razor 语法,我无法通过将它们传输到外部 js 文件来解决问题.
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.
我可以用它做什么?
谢谢.
更新:
最后我决定解决将我的 js 代码与视图分离的问题.所以新的问题是如何将 razor 语法包含到 js 文件中,或者什么是可接受的替代方案.我发现的流行解决方案是使用全局变量、数据属性和我更喜欢的解决方案——John Katsiotis 的 RazorJS 库.
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
我希望它能够稳定运行并让 Resharper 满意.
I hope it’s going to work stable and make Resharper happy.
干杯!
更新:
3 年后想起这个问题,决定根据我的经验更新一下.事实上,现在我宁愿不建议为此使用额外的库.尤其是如果您不是项目团队中的唯一成员……如果您在所有库中都得到保证,它们会得到创建者和社区的支持,并且可以轻松集成到您的 IDE 中(例如,如果使用特殊语法),那就更好了.此外,您团队中的所有人员都应该知道它是如何工作的.所以现在我建议做以下事情:
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:
- 将所有 JS 保存在单独的文件中.尽可能地隔离它.为其提供外部 API.
- 从您的视图调用 API 函数.
- 将所有 Razor 生成的 URL、文本消息、常量作为资源参数传递.
例如:
js 文件:
$.api.someInitFunction = function(resources){ ... }
查看:
<script>
$.api.someInitFunction({
urls: { myAction: '@Url.Action("MyAction", "MyController")' },
messages: { error: '@errorMessage' },
consts: { myConst: @myIntConst }
});
</script>
推荐答案
如果 Resharper 警告你这没什么大不了 ^_^
If Resharper warns you it's not a big deal ^_^
但如果我是你,我根本不会将 JavaScript 放在局部视图中.
因为部分视图可以多次插入到一个页面中,所以您的 JavaScript 会出现问题.
Because the partial view could be inserted in one page many times then you'll get an issues with your JavaScripts.
对于您的情况,如果您无法将 JavaScript 与 JS 文件分开,那么只需创建另一个 PartialView 并将这些脚本放入其中,然后将其呈现在您的主页中.
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 放在部分视图中可以吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!