asp控件中的Javascript代码管理

发布于 2024-09-30 05:57:07 字数 887 浏览 0 评论 0原文

我们正在寻找一种方法来管理我们的 asp 控件(与 RegisterClientScript 系列一起使用)不断增加的 javascript 代码。经典的方法是将 javascript 编码为 c# 代码中的字符串常量,但现在它变得越来越大,并且您不想在 c# 字符串中进行开发。将代码放在外部文件中并在编译之前将其转换/复制为字符串形式也有些荒谬。另外,在这些场景中,几乎相同的代码会随控件的每个实例发送多次。

其他重要标准是让脚本自给自足,仅外部依赖于 jquery&co,因此不需要在其他地方拥有特定于控件的 javascript 文件。所有相关部分必须位于一处。

到目前为止,我个人最好的选择是将 Javascript 分为两部分:
- “代码隐藏”之类的 js 文件被编译为嵌入资源到程序集中。这比字符串连接慢大约 3 倍。另外,它还提供了缓存和直接从文件选项,无需重新编译进行调试。此部分在包含控件的每个页面内联交付一次。
- 使用控制实例特定参数调用主脚本的微小初始化代理函数。这些也是内联传递的,但每个控制实例一次。

我确信我们不是唯一对此感到困惑的人。
那么你该怎么做呢?

更新。 摆脱.net细节,情况如下。 我们有大约 100 多个可重用的服务器端组件,每个组件都可能包含一些 JavaScript 客户端功能。当然,这些控件并不是在每个页面中都使用,因此不需要将 javascript 打包到一个公共库中。另一方面,拥有 20 多个对外部 javascript 文件的引用,每个文件的大小为 1-2Kb,这对于维护和流量过大来说也是令人头痛的。 这就是为什么我们希望组件了解并关心它自己的 javascript 包含。

另一方面,如果您有一个项目,其中这些组件分布在多个库中,您并不真的希望它们依赖于某些特定的文件系统布局,这当然可能是标准化的,但对车辆轮胎来说责任太大在我看来,强制你在餐馆里选择可乐。

We're looking for a way to manage ever-increasing javascript code for our asp controls (used with RegisterClientScript family). The classic way was to have javascript encoded into string constants inside c# code but now it's getting bigger and you don't want to develop inside c# string. Having code in external file and convert/copy it into a string form before compilation is also somewhat ridiculous. Plus in these scenarios almost same code is sent several times with each instance of the control.

Other important criteria is to have the scripts self-sufficient, with only external dependency on jquery&co, so having control-specific javascript files somewhere else is not desired. All the relevant parts have to be in one place.

My personal best option so far is to have Javascript split into two parts:
- "code behind"-like js file compiled as embedded resource into assembly. This is about 3 times slower than string concatenation but. Plus it brings options for caching and direct-from-file without recompiling for debugging. This part is delivered inline once per page containing the control.
- tiny initialising proxy function calling the main script with control-instance specific parameters. These are delivered also inline but once per control instance.

I'm sure we're not the only ones scratching our heads about this.
So how do you do that?

Update.
Getting rid of .net specifics, situation is following.
We have some 100+ reusable server-side components each possibly containing some javascript client-side functionality. These controls are of course not used in every page so packing the javascript into one common lib is not desired. On the other hand, having 20+ references to external javascript files each being 1-2Kb in size is also both headache for maintenance and traffic overkill.
That is why we want to have the component know and care about its own javascript inclusion.

On the other hand, if you have a project where these components are spread into several libraries, you don't really want for them to depend on some particular filesystem layout which may be of course standardised but it's too much of responsibility for a vehicle tire to enforce your cola choice in a diner, imo.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

横笛休吹塞上声 2024-10-07 05:57:07

如果您有由多个控件重复使用的 JavaScript 代码 - 为什么不希望将其放在外部 *.js 文件中?这将允许您保持格式,但仍然提供最小化、gzip 和客户端可缓存的文件。

If you have JavaScript code that is re-used by several controls - why don't you want this in an external *.js file? This will allow you to maintain formatting, and yet still serve up a minimized, gzipped and client side cachable file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文