<script src="scripts/require.js"></script><script>
require(["a.js","b.js","some/module"],function(){//This function will be called when all the dependencies//listed above are loaded. Note that this function could//be called before the page is loaded. This callback is optional.});</script>
//when the DOM is ready
window.addEvent('domready',function(){//find image to lazyload var scrollspyElements = $$('.lazyload');//if there are images to lazyload... if(scrollspyElements.length){//require the class and when done...
require(['lazyload.js'],function(){//create a LazyLoad instance and use it! new LazyLoad({
range:200,
image:'Assets/blank.gif',
elements: $$('.lazyload')});});}});
//load mootools with RequireJS!
require(['mootools-1.2.4.js'],function(){//when the DOM is ready
require.ready(function(){//find image to lazyload var scrollspyElements = $$('.lazyload');//if there are images to lazyload... if(scrollspyElements.length){//require the class and when done...
require(['lazyload.js'],function(){//create a LazyLoad instance and use it! new LazyLoad({
range:200,
image:'Assets/blank.gif',
elements: $$('.lazyload')});});}});});
真棒!按需加载我们需要的组件是JavaScript的未来(甚至是整个架构)。
require.ready!
require.ready()是指在 DOM 加载完毕之后执行代码,就像 jQuery 中的 $(document).ready()。
如果你不使用JavaScript框架,RequireJS提供了一个现成的方法来触发DOM is ready!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// require ScrollSpy
require(['scrollspy.1.2.js','Roar.js'],function(){//use it when the dom is ready!
require.ready(function(){//use them!var roar =new Roar();var spy =new ScrollSpy({
container: document.id('someDiv'),
onEnter:function(){//....}});});});