简体   繁体   English

使用多个Javascript框架修复项目

[英]Fixing a project with multiple Javascript frameworks

I just started a new job, and my first task is to clean up the Javascript code for the site - the problem is that there are two JS libraries being used (jQuery and Prototype / Scriptaculous). 我刚刚开始一项新工作,我的第一个任务是清理网站的Javascript代码-问题是有两个正在使用的JS库(jQuery和Prototype / Scriptaculous)。

I'm trying to figure out what's being used where, but it's pretty difficult, especially not being very familiar with the code. 我试图弄清楚在什么地方使用了什么,但是这非常困难,尤其是对代码不太熟悉。

Does anyone have any suggestions? 有没有人有什么建议? I'd probably prefer to drop Prototype / Scriptaculous if possible. 如果可能的话,我可能更希望删除Prototype / Scriptaculous。

I wouldn't suggest to drop a javascript library, as this would be a cross-sectional change. 我不建议删除javascript库,因为这将是横截面更改。 Very dangerous, and will cause spurious bugs now and in the future. 非常危险,现在和将来都将导致虚假错误。

Chances are, that Prototype will be using the $ variable, and that jQuery will be running in noConflict mode, so it will be referenced by the jQuery variable. 可能是,Prototype将使用$变量,并且jQuery将在noConflict模式下运行,因此它将由jQuery变量引用。

It can get tricky, for instance: 例如,它可能会很棘手:

 // here $ = Prototype

 jQuery(function($){
     // here $ = jQuery

 });

 // here $ = Prototype

And in separate files: 并在单独的文件中:

// here $ = Prototype
(function($){
    // here $ = jQuery;
})(jQuery);
// here $ = Prototype

Its just really important to pay attention to your context to see which library is being referenced. 注意您的上下文以查看被引用的库确实非常重要。 Additionally, its perfectly acceptable to ask questions here if you get stumped on how to do something in jQuery that was done in Prototype. 此外,如果您对如何在Prototype中完成的jQuery操作感到困惑,可以在这里提出问题是完全可以接受的

Getting rid of Prototype in particular could be wicked hard, especially if there were people working on the codebase who really "got" Prototype. 尤其是如果有一些在代码库上工作的人真正“摆脱”了原型,那么摆脱Prototype可能会非常困难。 I'm speaking here about myself, having worked on a very large web application that in fact has exactly the mix you describe (although I think that by the time I left the Scriptaculous stuff was gone). 我在这里谈论我自己,是在一个非常大的Web应用程序上工作的,实际上它确实具有您所描述的混合方式(尽管我认为到我离开Scriptaculous的时候已经不见了)。

Because of the way Prototype works, there can be dependencies absolutely everywhere. 由于原型的工作方式,绝对可以在任何地方都存在依赖项。

This is a bit of a daunting task. 这是一项艰巨的任务。 The suggestion to live and let live both frameworks is not a bad one, provided things are working. 只要一切正常,关于活着和放任两个框架的建议都不错。 It is likely jQuery is in noConflict mode if they are really resident on the same page. 如果jQuery确实位于同一页面上,则很可能处于noConflict模式。

One tool that's very useful is to use the Web Developer Toolbar for Firefox and navigate to a page and do: Information -> View JavaScript -- this will give you a full listing of all the JavaScript script loaded with <script> tags, as well as inline JavaScript in <script> tags, though I think inline event handlers are not listed there. 一个非常有用的工具是使用Firefox的Web开发人员工具栏并导航到页面并执行以下操作: Information -> View JavaScript这还将为您提供加载了<script>标记的所有JavaScript脚本的完整列表。作为<script>标记中的内联JavaScript,尽管我认为内联事件处理程序未在此处列出。

But one thing you can do is simply unhook Prototype or JavaScript and see which ends you up with fewer errors. 但是,您可以做的一件事就是简单地摘除Prototype或JavaScript,然后查看哪个最终可以减少错误。 Then you're on your way to debugging what's missing. 然后,您便可以调试缺少的内容。

It is easier to standardize on one library, but there's a case to be made for "if it ain't broke, don't fix it" too. 比较容易在一个库规范,但有一个为“如果没坏,就不要修理它”太进行的情况下。

I've taken some time to learn a bit about all frameworks, doing the same effects on identical HTML: ArtLung Rosetta : You can get a quick dive into the differences between syntaxes of Prototype and jQuery by comparing this and this . 我花了一些时间来了解所有框架,并在相同的HTML上实现了相同的效果: ArtLung Rosetta :通过比较thisthis,您可以快速了解Prototype和jQuery语法之间的区别。

Best of luck! 祝你好运!

I would trash prototype from all includes on all pages, remove the noconflict in jquery & then keep firebug ready to show me all errors, and go over all the pages one by one, replacing functions with jquery equivalents, until things work right. 我会从所有页面上所有包含的原型中删除原型,删除jquery中的noconflict,然后让firebug准备向我展示所有错误,然后逐页浏览所有页面,用jquery等效项替换功能,直到一切正常。

..pain in the butt method, but doubt you'll find less painful. ..痛苦的对接方法,但怀疑您会不会觉得那么痛苦。 Might be helpful: http://api.jquery.com/browser/ 可能会有所帮助: http : //api.jquery.com/browser/

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

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