繁体   English   中英

用dojo AMD加载条件模板(html)

[英]Conditional template(html) loading with dojo AMD

所以我有这个简单的要求:

require(['app/'+url,'dojo/text!content/'+url], function(module,template){
        if(module.init){
                module.init({
                        container: panel, 
                        containerId: 'contentTabs_' + idOp,
                        template: template
                });
        }
});

但是模板可能不存在(有时确实存在)。 因此,无论发生什么情况,我都希望执行我的require回调。

AMD如何做到这一点?

多谢你们。

而不是使用dojo/text! ,编写您自己的插件:

require(['foo/maybe!'+url], function(maybeTemplate) {
  if (maybeTemplate === undefined) {
    // it's there
  } else {
    // it's not there
  }
}

foo/maybe必须解析为具有load : function (id, require, callback)成员的对象。

 load( id, // the string to the right of the ! require, // AMD require; usually a context-sensitive require bound to the module making the plugin request callback // the function the plugin should call with the return value once it is done ) -> undefined 

您可以使用XHR调用来获取资源,并在出现404错误时解析为undefined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM