2013-02-23 71 views
0

我在使用下劃線js和骨幹js時遇到了一些問題。我能夠顯示純粹的HTML骨幹,但我無法獲得模板顯示。在下劃線js和骨幹js中實現模板時出錯je

模板:

<script type="text/template" id="edit-user-template"> 
     <form class="edit-user-form"> 
      <legend>Create User</legend> 
      <label>First Name</label> 
      <input type="text" name="firstname" /> 
      <label>Last Name</label> 
      <input type="text" name="lastname" /> 
      <label>Age</label> 
      <input type="text" name="age" /> 
      <hr /> 
      <button type="submit" class="btn">Create a User</button> 
     </form> 
</script> 

查看:

var EditUser = Backbone.View.extend({ 
       el: '.page', 
       template: _.template($('#edit-user-template').html),     
       render: function() {  
       this.$el.html(this.template({})); 
       return this; 
       } 
      }); 

路線:

var Router = Backbone.Router.extend({ 
       routes:{ 
        '':'home', 
        'new':'editUser' 
       } 
      }); 

      var userList = new UserList(); 
      var editUser = new EditUser(); 

      var router = new Router(); 
      router.on('route:home', function(){ 
       userList.render(); 
      }); 
      router.on('route:editUser', function(){ 
       editUser.render(); 
      }); 
      Backbone.history.start(); 

當我嘗試在瀏覽器中加載該頁面出現錯誤:

Uncaught TypeError: Object function(e){return b.access(this,function(e){var n=this[0]||{},r=0,i=this.length;if(e===t)return 1===n.nodeType?n.innerHTML.replace(gt,""):t;if(!("string"!=typeof e||Tt.test(e)||!b.support.htmlSerialize&&mt.test(e)||!b.support.leadingWhitespace&&yt.test(e)||At[(bt.exec(e)||["",""])[1].toLowerCase()])){e=e.replace(vt,"<$1></$2>");try{for(;i>r;r++)n=this[r]||{},1===n.nodeType&&(b.cleanData(Ot(n,!1)),n.innerHTML=e);n=0}catch(o){}}n&&this.empty().append(e)},null,e,arguments.length)}has no method 'replace' underscore-min.js:1

任何想法是什麼造成這種情況?

回答

1

jQuery.html()是一個方法,所以不是這樣的:

template: _.template($('#edit-user-template').html) 

你需要

template: _.template($('#edit-user-template').html())