How to achieve lazy loading with RequireJS?

后端 未结 2 649
抹茶落季
抹茶落季 2020-12-04 09:08

We\'re building a non-trival web application using Backbone, RequireJS and Handlebars, and well, I\'m just curious. At the moment, each of our models sorta looks like this:<

2条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 10:02

    You may also want to check out require-lazy.

    It has a runtime component and a build time component. The runtime component allows you to lazily require a module as (note the lazy! plugin):

    define(["lazy!mymodule"], function(mymodule) {
        ...
    });
    

    In the previous context, mymodule is a promise, the real module will be loaded with get() and will be made available in the then() callback:

    mymodule.get().then(function(m) {
        // here m is the real mymodule
    });
    

    Require-lazy integrates with r.js to automatically create "bundles" of Javascript files. It also handles automatically cache-busting for the bundles. There are several examples to get an idea. There is also Grunt and Bower integration.

提交回复
热议问题