如何有效地将 yepnope.js 与 $(document).ready() 一起使用?
我一直在实现 yepnope 脚本加载器作为现代化库的一部分。之后我已成功加载 jQuery 和 jQuery 相关脚本。我对异步加载资源很陌生,所以这对我来说有点新鲜。我一直在四处寻找,但对以下内容没有太多运气。
我的问题是,您对使用 yepnope.js 框架时如何有效替换 $(document).ready() 的功能有何看法。
我的理论是在我的基础库中创建一个适当命名的函数,然后将页面上的变量设置为包含我现有的 $(document).ready() 代码的匿名函数。在完整回调中加载所有脚本后,yepnope 将调用此变量。
您是否同意这是一个很好的方法,或者我是否以完全错误的方式处理这个问题?
(对于那些不知道的人来说,yepnope.js 的异步特性意味着文档在 yepnope 加载程序完成之前调用 $ 或 jQuery,抛出“$ 未定义”错误 <- 如果这是错误的,请纠正我。)
第一个问题,希望这是一本好书。
I have been implementing the yepnope script loader as part of the modernizr.js library. I have successfully got jQuery to load and jQuery dependent scripts afterwards. I am new to asynchronous loading of resources, so it's a bit new to me. I have been searching around, but haven't had much luck with the following.
My question is what are your opinions on how to effectively replace the functionality of $(document).ready() when working with the yepnope.js framework.
My theory was to create a appropriately named function in my base library and then set that variable on my pages to an anonymous function containing my existing $(document).ready() code. This variable would then be called by yepnope after all the scripts had loaded in the complete callback.
Would you agree that this is a good way of doing it, or am I approaching this entirely the wrong way?
(For those unaware, the asynchronous nature of yepnope.js means that the document calls $ or jQuery before the yepnope loader has finished, throwing a "$ is undefined" error <- please correct me if that is wrong.)
First question, hope it's a good one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果不使用 yepnope 加载 jQuery 对您来说不是问题,那么还有一种更简单的方法可以实现。
If load jQuery without yepnope isn't a problem for you, there is a easier way to do.
这是我使用的技术。它允许我在任何我喜欢的地方进行 $(document).ready() 风格的调用。使用此方法,您可以采用已使用 jQuery 并具有现有 $(document).ready() 调用的站点,并轻松改造 yepnope。
首先,添加这行 JS,最好是在文档头中,在调用 $(document).ready() 的任何 javascript 之前:
然后,将 yepnope jQuery 测试对象设置与此类似:
我们创建一个假 $(document)。在 jQuery 加载之前准备好()。这将每个 $(document).ready() 调用存储在数组 docready 中。然后,一旦 jQuery 加载完毕,我们就用现在加载的真实 jQuery 对象覆盖临时 $ 对象。然后,我们迭代所有存储的 $(document).ready() 调用,并真正执行它们。
更新:Chris Jones 的改进版本,还涵盖 $(function() {}) 样式调用。
This is the technique I use. It allows me to sprinkle $(document).ready() style calls wherever I like. Using this method, you can take a site that already uses jQuery and has existing $(document).ready() calls, and easily retrofit yepnope.
First, add this line of JS, preferably in the document head, before any javascript that calls $(document).ready():
Then, have your yepnope jQuery test object set similar to this:
We create a fake $(document).ready() before jQuery loads. This stores every $(document).ready() call in an array, docready. Then, once jQuery has loaded, we overwrite our temporary $ object with the now loaded real jQuery object. Then, we iterate through all the stored $(document).ready() calls, and execute them for real.
UPDATED: improved version from Chris Jones that also covers $(function() {}) style invocations.
脚本标记是同步加载的 - 因此,如果您将 yepnope 放入 js 文件中并通过 script 标记加载它:
就在结束正文标记之前,您可以肯定处于 $(document).ready() 状态。
您需要自己回答的是,强制 yepnope 以 $(document).ready() 方式加载是否有意义,因为它的主要目的是首先打破脚本标记的同步加载顺序。
script tags are loading synchronously - so if you put your yepnope in a js file and load it via script tag:
right before the closing body tag you can be quite sure to be at $(document).ready() state.
What you need to answer for yourself is whether it makes sense to force yepnope to load in a $(document).ready() fashion, as its main purpose is to break the synchronous loading order of script tags in the first place.
根据 @ezmilhouse 的指导,我思考了实现我所追求的目标的最佳方法,同时仍然保持与旧代码的兼容性。
我的解决方案是设置我的 yepnope 脚本加载器,根据其各自的依赖关系以分层方式加载所有必要的脚本。加载所有脚本后,您可以使用我调用 yepnope 的
complete
属性来调用我的 Ready 函数。这意味着该文档已有效准备就绪,并且代码可以毫无问题地运行。我还将我的 js 移到了页面的底部(这是我很久以前就应该做的事情,但我们有很多遗留页面!:))
这是一个示例(使用错误的库/脚本名称仅用于说明目的):
在我的页面 js 中,我将向
baseLib.Page.Ready
分配一个函数,然后 yepnope 在完成时调用该函数。Using guidance from @ezmilhouse, I thought about the best way to achieve what I was after while still keeping compatibility with our older code.
My solution was to set up my yepnope scriptloader to load all necessary scripts in a hierarchical fashion, based on their individual dependencies. Once all scripts are loaded, you can use the
complete
property of my call to yepnope to call my ready function. This meant that the document was effectively ready and the code would work with no issues.I also moved my js to the base of my pages (something that I should have done a long time ago, but we had a lot of legacy pages! :) )
Here is an example (using false libray/script names for illustration purposes only):
In my page js I will assign a function to
baseLib.Page.Ready
that will then be called by yepnope on complete.我认为 Alex Sexton 解决方案 是正确的:
I think that Alex Sexton solution would be correct :