requirejs定义嵌套依赖项顺序

时间:2017-08-18 23:22:00

标签: javascript requirejs

只看例子如下:

档案a.js

define({name:'a'});

文件b.js

define(['jquery','a'],function($,a){
    let local=$.extend({customForB:'b'},a);// at the point a is used
    return function(options){return $.extend({},local,options)};
});

file c.js

define(['jquery','a'],function($,a){
    let local=$.extend({customForC:'c'},a);// at the point a is used
    return function(options){return $.extend({},local,options)};
});

档案d.js

define(['jquery','a'],function($,a){
    a.name='global change';//effect to b and c or other dependencies
    require(['b','c'],function(b,c){
         //do nothing, but the local.name change 
         $.b=b;
         $.c=c;
    }
    $.a=a;
    return $;
});

file test.html

<script>
     require(['d'],function($){
          // At this point, it is uncertain whether B and C are loaded, 
          // is there a way to make sure that B and C are loaded, 
          // Can i use plugins to change the contents of the a?
          // At the same time affect its dependence       
     });
</script>

同样如下

档案d.js

define(['jquery','a'],function($,a){
    a.name='global change';//effect to b and c or other dependencies
    $.a=a;
    return $;
});

file test.html

<script>
     require(['d'],function($){
          require(['b','c'],function(b,c){
              $.b=b;
              $.c=c;
              //It's more complicated than the code above,But the functions are the same
         }
     });
</script>

我别无选择,只能添加一些额外的字以避免验证

0 个答案:

没有答案