问题描述
我希望将 javascript 代码与视图分开.
我需要为 javascript 生成的简单图像按钮实现本地化:
i want javascript code to be separated from views.
i got the requirement to implement localization for a simple image button generated by javascript:
本地化标题的最佳技术是什么?
what's the best technique to localize the title of it?
ps:我发现 ayende 的百家乐凯发k8的解决方案.这是正确的方向.
ps: i found a solution by ayende. this is the right direction.
我得到了提供 controller.resource('foo') 扩展方法的本地化助手类.
i got localization helper class which provides the controller.resource('foo') extension method.
我正在考虑扩展它(帮助程序),以便它可以按名称返回指定控制器的所有 javascript 资源(来自 app_localresources 中的clientsideresources"子文件夹).然后 - 在 basecontroller 中调用它,将它添加到 viewdata 并在 layout 中渲染它.
i am thinking about to extend it (helper) so it could return all javascript resources (from "clientsideresources" subfolder in app_localresources) for the specified controller by its name. then - call it in basecontroller, add it to viewdata and render it in layout.
这是个好主意吗?
推荐答案
编辑
考虑将必要的本地化资源写入 javascript 对象(哈希),然后使用它来查找动态创建的对象.我认为这比回到服务器进行翻译要好.这类似于通过 viewdata 添加它,但可能更灵活一些.fwiw,我可以将本地化资源视为视图的一部分,而不是控制器的一部分.
consider writing the necessary localized resources to a javascript object (hash) and then using it for lookup for your dynamically created objects. i think this is better than going back to the server for translations. this is similar to adding it via viewdata, but may be a little more flexible. fwiw, i could consider the localization resources to be part of the view, not part of the controller.
在视图中:
会输出类似的东西:
var local = {}; local.close = "close"; local.open = "open";
如果没有参数,它将输出整个翻译哈希.使用参数使您能够为每个视图自定义它.
without arguments it would output the entire translation hash. using arguments gives you the ability to customize it per view.
然后您可以在 javascript 文件中使用它,例如:
you would then use it in your javascript files like:
$(function(){ $('#button').click( function() { $("") .appendto("#somediv") .click( function() { ... } ); }); });
实际上,只要 javascript 代码在容器中本地化,我就不会太在意将 javascript 代码排除在我的视图之外.通常我会设置我的母版页有 4 个内容区域:标题、页眉、主要和脚本.标题、页眉和主要内容位于您所期望的位置,而脚本区域位于正文的底部.
actually, i'm not too fussed about keeping my javascript code out of my views as long as the javascript code is localized in a container. typically i'll set my master page up with 4 content area: title, header, main, and scripts. title, header, and main go where you would expect and the scripts area goes at the bottom of the body.
我将所有的 javascript 包括在内,包括任何用于 viewusercontrols 的内容,都放入脚本容器中.特定于视图的 javascript 代码位于包含之后.我根据需要将共享代码重构回脚本.我曾考虑使用控制器方法来整理脚本包含,即使用单个请求包含多个脚本,但尚未解决.
i put all my javascript includes, including any for viewusercontrols, into the scripts container. view-specific javascript code comes after the includes. i refactor shared code back to scripts as needed. i've thought about using a controller method to collate script includes, that is, include multiple scripts using a single request, but haven't gotten around to that, yet.
这样做的好处是可以将 javascript 代码分开以提高可读性,但也允许我根据需要轻松地将模型或视图数据注入到 javascript 代码中.
this has the advantage of keeping the javascript code separate for readability, but also allows me to easily inject model or view data into the javascript code as needed.