[英]Using jQuery.load with RequireJS
I often bootstrap widgets I write like so: 我经常引导我编写的小部件,如下所示:
$(window).load(function () {
doStuff();
});
This is often the case as I write widgets that are dependent on the rest of the application to be initialized. 当我编写依赖于要初始化的应用程序其余部分的小部件时,通常就是这种情况。
I recently started using RequireJS and am not able to figure out how to attach a callback as in the code above. 我最近开始使用RequireJS,但无法像上面的代码那样弄清楚如何附加回调。 I looked at and tried the domReady plugin however, it won't fit my use-case since the callbacks are invoked when the DOM is ready and not when all resources are loaded. 我查看并尝试了domReady插件,但是它不适合我的用例,因为在DOM准备就绪时而不是在加载所有资源时调用了回调。
Here is what I have tried so far: 到目前为止,这是我尝试过的:
// Using domReady (main.js):
require(['require', 'domReady'], function (require, domReady) {
'use strict';
domReady(function () {
require(['app'], function (App) {
console.log(window);
App.initialize();
});
});
});
// Using jQuery (main.js)
// Note, this does not work.
require(['jquery', 'app'], function ($, App) {
'use strict';
$(window).load(function () {
App.initialize();
});
});
Given the above, what is the correct way to achieve using $(window).load()
with RequireJS? 鉴于以上所述,在RequireJS中使用$(window).load()
的正确方法是什么?
If I understand you correct you can check $.holdReady()
method to acheive the result; 如果我理解您的$.holdReady()
正确的,则可以检查$.holdReady()
方法来获得结果; so just after load jquery run 所以在加载jQuery运行后
jQuery.holdReady(true);
later attach your handlers which should run only after load with 稍后附加您的处理程序,该处理程序应仅在加载后运行
jQuery(function($) {
App.initialize(); //will run only after window.load;
}
and in your load handler setup: 并在您的负载处理程序设置中:
jQuery(window).load(function () {
jQuery.holdReady(false); //fire all domready holded events
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.