Javascript 把手:组织和访问远程模板

Javascript 把手:组织和访问远程模板,javascript,jquery,templates,handlebars.js,Javascript,Jquery,Templates,Handlebars.js,我想在不同的HTML文件中组织我的HB模板。因此,我有helpers.html,它包含了一个想法:尝试使用它们,如果你没有找到你想要的东西,就加载它 这是一个简单的示例,但您可以通过更复杂的解决方案变得更聪明: function useAlert(){ var el = $('#alert'); // in case el is available carry on if(el){ // do whatever you want } else { // here

我想在不同的HTML文件中组织我的HB模板。因此,我有helpers.html,它包含了一个想法:尝试使用它们,如果你没有找到你想要的东西,就加载它

这是一个简单的示例,但您可以通过更复杂的解决方案变得更聪明:

function useAlert(){
  var el = $('#alert');
  // in case el is available carry on
  if(el){
    // do whatever you want
  } else {
    // here we're loading the templates inside an element with id #templates'
    // in the callback we're using the recursion to call again this method
    $('#templates').load('/path/to/helpers.htm', function(error){
      // debug... remove the console log in production
      if(error){
        return console.log('Error', error);
      }
      // try again now
      useAlert();
    });
  }