簡體   English   中英

車把:組織和訪問遠程模板

[英]Handlebars: Organizing and accessing remote templates

我想在不同的HTML文件中組織我的HB模板。 因此,我有helpers.html ,其中包含<script id='alert' type='text/template>...</script> helpers.html <script id='alert' type='text/template>...</script><script id='notification' type='text/template'></script> ,那么我將如何訪問該文件中的特定模板? 使用Jquery,我們可以執行類似$('#alert') ,但這是一個遠程模板...甚至可能嗎?

想法:嘗試使用它們,如果找不到所需內容,請加載它。

這是一個簡單的示例,但是您可以通過更復雜的解決方案來提高自己的智慧:

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();
    });
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM