簡體   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