使用jQuery插件及其依赖项的指南 [英] Guidelines for using jQuery plugins and their dependencies

查看:113
本文介绍了使用jQuery插件及其依赖项的指南的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery插件通常依赖于外部文件:jQuery库,样式表(CSS),图像,其他插件等。使用(和编写)jQuery插件可以解决依赖关系放置的指导原则是什么?换句话说,所需文件应该放在哪里:在主应用程序文件夹(Img,Css,JS或您使用的任何文件夹)下,在插件文件夹下(例如插件/ MyPlugin / Img,插件/ MyPlugin / Css等),或其他什么?

jQuery plugins often have dependencies on external files: jQuery library, style sheets (CSS), images, other plugins, etc. What are the guidelines for using (and writing) jQuery plugins that would address the placement of the dependencies? In the other words, where should the required files go: under main app folders (Img, Css, JS, or whatever folders you use), under plugin folder (e.g. Plugins/MyPlugin/Img, Plugins/MyPlugin/Css, etc), or something else?

在项目中包含几个插件后,我担心其他项目成员将很难确定需要哪些依赖项以及哪些文件应该去哪里。

After including several plugins to the project, I'm concerned that other project members will have a hard time figuring out which dependencies are required and which files should go where.

哪些有效,哪些无效?

推荐答案

<个人而言,当我构建一个插件时,我尝试尽可能少地依赖其他资源来避免这个问题。有时您无法避免使用外部样式表,在这种情况下,我总是将它们与其他CSS文件一起使用,即使它们最初包含在与插件相同的文件夹中。这样可以避免在尝试确定哪些样式表应该去哪里时出现任何歧义。无论如何,您将手动包含它们。如果插件有任何依赖关系,它们会进入JS文件夹,其组织方式与其他插件类似。在这种情况下,图像将与所有其他图像一起使用。

Personally, when I build a plugin, I try to make it dependent as little as possible on other resources as to avoid this issue. Sometimes you can't avoid using external stylesheets in which case I've always put them with my other CSS files even if they are originally included in the same folder as the plugins. This avoids any ambiguity when trying to decide which stylesheets should go where etc. You will manually have to include them anyways. If the plugin has any dependencies, they go in the JS folder, organized in a similar manner as the other plugins. Images, in this case, would then go with all of the other images.

在构建插件时,您可以通过允许用户定义类来使其更灵活应用于某些对象,或让用户定义插件将操作的结构。所有这一切都可以在提供一组良好的默认值时完成,同时尽可能少地依赖外部资源。

When building a plugin, you can make it more flexible by allowing the user to define the classes that are applied to certain objects, or let the user define the structure of what the plugin will manipulate. All of this can be done while giving it a set of good defaults to follow while relying as little as possible on external resources.

关于是否已定义最佳实践这些情况,我还没有找到。我只在jQuery网站上找到了插件创作指南: http://docs.jquery.com/Plugins/创作

As to whether best practices have been defined for these situations, I have not found any yet. I have only found the plugin authoring guidelines on the jQuery site: http://docs.jquery.com/Plugins/Authoring.

编辑:

关于插件的说明依赖组织:

As to clarify about plugin dependency organization:

当你说你有jquery.x.js和jquery.y.js时。它们都依赖于jquery.z.js。我总是将jquery.z.js放在与jquery.x.js和jquery.y.js相同的文件夹中。这避免了重复以及与违反组织惯例相关的任何混淆。所以:

When say you have jquery.x.js and jquery.y.js. They both depend on jquery.z.js. I have always put jquery.z.js in the same folder as jquery.x.js and jquery.y.js. This avoids duplication and any confusion related to breaking the organizational convention. So:


  • ./ jquery.x.js

  • ./ jquery.y.js

  • ./ jquery.z.js

我通常组织我的文件夹:

I normally organize my folders as such:


  • ./ js / jquery-xxxjs

  • ./ js / plugins / jquery.x.js

  • ./ js / plugins / jquery.y.js

  • ./ js / plugins / jquery.z.js

  • ./js/jquery-x.x.x.js
  • ./js/plugins/jquery.x.js
  • ./js/plugins/jquery.y.js
  • ./js/plugins/jquery.z.js

这篇关于使用jQuery插件及其依赖项的指南的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆