简体   繁体   English

在需要时或始终通过外部加载jQuery / CSS内联?

[英]Loading jQuery / CSS inline when needed or always via external?

I like keeping myself busy building modular web-applications, but don't want to spend time where I can save some.. 我喜欢让自己忙于构建模块化的Web应用程序,但不想花时间在哪里可以保存一些...

For example I'm building a news module that should be easily implemented over multiple sites, because the same web-application is used. 例如,我正在构建一个应该可以在多个站点上轻松实现的新闻模块,因为使用了相同的Web应用程序。

However not all websites will need a news module. 然而,并非所有网站都需要新闻模块。 Is it better/easier/faster to create an inline stylesheet/javascript file built into the module itself, than to create a big external stylesheet/javascript with all the libraries? 是否更好/更容易/更快地创建内置于模块本身的内联样式表/ javascript文件,而不是创建一个包含所有库的大型外部样式表/ javascript? Even though the file for the news module is not needed on all other webpages? 即使所有其他网页都不需要新闻模块的文件?

It seems to be much easier to create an inline library in the module itself. 在模块本身中创建内联库似乎要容易得多。 So that this only gets loaded when needed, and saves load time and bandwidth on the other pages. 这样只在需要时加载,并在其他页面上节省加载时间和带宽。

The other thing is that I like writing 'plug-and-play' modules. 另一件事是我喜欢编写“即插即用”模块。 Say I move a file across the file server into the module folder, and the application will take care of the rest. 假设我将文件跨文件服务器移动到模块文件夹中,应用程序将负责其余部分。 With inline sheets, I dont have to add new lines to the header/footer etc. 使用内联工作表,我不必在页眉/页脚等中添加新行。

What is the best solution for this? 什么是最好的解决方案? When also taking into account that I rather spend 10 minutes moving a file and it works, than to spend 1 hour appending external libraries just because its more of a 'good practice'? 还考虑到我宁愿花10分钟移动一个文件并且它有效,而不是花1小时追加外部库只是因为它更像是一个“好习惯”?

If you re building web applications, are you using the MVC patern? 如果您正在构建Web应用程序,您使用的是MVC模式吗? Do you separate your concerns? 你把你的顾虑分开吗? (your Templates/Views, your Models, and your logic(Controllers)) (您的模板/视图,模型和逻辑(控制器))

If you follow MVC, it makes easier maintaining and customizing your app. 如果您关注MVC,则可以更轻松地维护和自定义您的应用。

To answer to your exact question, what you need is RequireJS . 要回答您的确切问题,您需要的是RequireJS This way you have only one place to declare your requirements, and RequireJS will handle the rest.. Load order and more.. 这样你只有一个地方可以声明你的要求,而RequireJS将处理其余的..加载订单等等..

Quoting from the requirejs website: 引用requirejs网站:

Over time, if you start to create more modular code that needs to be reused in a few places, the module format for RequireJS makes it easy to write encapsulated code that can be loaded on the fly. 随着时间的推移,如果您开始创建需要在少数地方重用的更多模块化代码,RequireJS的模块格式可以轻松编写可以即时加载的封装代码。

Inline is never a solid way to maintain CSS. 内联永远不是维护CSS的可靠方法。 Take care to separate the description of your layout from your views. 请注意将布局描述与视图分开。 You can easily include the file in the same directory as the module so it should not be an issue. 您可以轻松地将该文件包含在与模块相同的目录中,因此它不应成为问题。

Or if you want just to load "on fly" some script you can use jQuery.getScript , http://api.jquery.com/jQuery.getScript/ . 或者,如果您只想加载“on fly”某些脚本,可以使用jQuery.getScript, http: //api.jquery.com/jQuery.getScript/。 Otherwise you should follow George's way. 否则你应该遵循乔治的方式。

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

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