开发者

requirejs plugin loads module in fresh context

I want to have a module load multiple times on a page, with a unique context for 开发者_Go百科each instance. There are two normal ways to do this.

  1. the module can return a constructor
  2. i can explicitly run the require method, providing a context id.

Either of these would work in my situation, but I want a third option. :)

I want a requirejs plugin that will return a module to me in a fresh context. ie,

require(["new!some/module"], function(SomeModule) {
    // SomeModule, here, is in its own context.  If i were to run this
    // method call again, SomeModule would be in a new context.
});

I've started looking into building a plugin to do this...

load: function (name, req, load, config) {
    var index = get_unique_index();
    req({context:index}, [name], function(value) {
        if(!config.isBuild){
            load(value);
        }
    });
}

but the {context:index} dict doesn't work here... thoughts?


Instead of using the local req passed to load, use the global require function, just called require(). The local one passed to load is already bound to a context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜