简体   繁体   English

使用Razor语法的Javascript名称空间?

[英]Javascript namespaces with Razor syntax?

I use this "Javascript namespace" pattern a lot in my script snippets embedded within .cshtml files (partial views): 我在嵌入在.cshtml文件(部分视图)中的脚本片段中经常使用这种“ Javascript名称空间”模式:

 <script type="text/javascript"> (function (MyNamespace) { MyNamespace.PublicFunc1 = function (p1, p2) { localFunc1('abc'); MyNamespace.PublicFunc2(p1, p2, 'abc'); } MyNamespace.PublicFunc2 = function (p1, p2, p3) { // ... } localFunc1(p1) { // ... } }(window.MyNamespace = window.MyNamespace || {})); </script> 

It works like a charm and I can call these MyNamespace.PublicFunc1 and MyNamespace.PublicFunc2 functions from outside that script snippet. 它的工作原理就像一个MyNamespace.PublicFunc1 ,我可以从该脚本片段的外部调用这些MyNamespace.PublicFunc1MyNamespace.PublicFunc2函数。

The problem arises when two or more of those partial views need to be rendered at the same time, so any external JS code making calls to, say, MyNamespace.PublicFunc1 will no longer be able to do so, because there would be two or more instances of that function. 当需要同时渲染两个或多个部分视图时,就会出现问题,因此,任何外部JS代码调用MyNamespace.PublicFunc1都将无法再进行,因为将有两个或多个该函数的实例。

To solve this, I tried using server-side unique namespace IDs/names for those scripts via Razor syntax, ie: 为了解决这个问题,我尝试通过Razor语法为这些脚本使用服务器端唯一的名称空间ID /名称,即:

 @{ // server-side variable with a unique namespace identifier/name string _namespace = Model.MyUniqueNamespace; } <script type="text/javascript"> (function (@_namespace) { @_namespace.PublicFunc1 = function (p1, p2) { @_namespace.PublicFunc2(p1, p2, 'abc'); } @_namespace.PublicFunc2 = function (p1, p2, p3) { // ... } localFunc1(p1) { // ... } }(window.@_namespace = window.@_namespace || {})); </script> 

The problem with this approach is that the entire @_namespace.PublicFunc1 part is treated by Razor and server-side -- which makes sense, but is not what I need; 这种方法的问题在于,整个@_namespace.PublicFunc1部分都由Razor和服务器端处理,这很有意义,但不是我所需要的。 I only need the @_namespace part to be server-side and everything else around it to be client-side/JS. 我只需@_namespace部分作为服务器端,并将其周围的所有其他内容作为客户端/ JS。

My questions are: 我的问题是:

  1. Is there a way to solve this using Razor syntax? 有没有办法使用Razor语法解决此问题?
  2. Is there another/better way to handle these scenarios (via Javascript magic), without having to write a full-fledged JS plugin/widget/library (would seem like too much for each such localized/page-specific script snippet)? 是否有另一种/更好的方式(通过Javascript魔术)来处理这些情况,而不必编写完整的JS插件/小部件/库(对于每个这样的本地化/特定于页面的脚本片段来说似乎太多了)?

Changing it to @(_namespace) should work for question 1. For question 2, it's important to note that the variable name you pass into the function doesn't... really... matter. 将其更改为@(_namespace)应该适用于问题1。对于问题2,请务必注意,传递给函数的变量名称并不……确实……很重要。 ie: 即:

(function(foo) {
  foo.func1 = function() { ... }
})(window.bar = window.bar || {});

this will still set window.bar.func1 . 这仍然会设置window.bar.func1

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

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