Условная загрузка шаблона (html) с помощью dojo AMD

StackOverflow https://stackoverflow.com//questions/11695420

  •  12-12-2019
  •  | 
  •  

Вопрос

Итак, у меня есть это простое требование:

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

Но шаблона может не быть (иногда он есть).Поэтому, что бы ни случилось, я бы хотел, чтобы мой обратный вызов был выполнен.

Как 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 для получения ресурса и разрешения undefined по ошибке 404.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top