2012-01-31 93 views
0

誰能告訴我爲什麼這段代碼會在var myview = new view();處拋出一個「undefined不是函數」的錯誤?我一直在尋找所有,我很確定一切都是正確的。錯誤:「undefined不是函數」backbone.js

router = Backbone.Router.extend({ 
    routes: { 
     '/': 'project' 
    }, 

    project: function() { 

     projects = new models.Projects(); 

     var view = Backbone.View.extend({ 
      el: 'body' , 
      render : function() { 
       $(this.el).empty().append(templates.Project({content : 'this works!' })); 
       return this; 
      } 
     }); 
     console.log(view); 
     /* 
     * Output: 
     * { [Function] 
       extend: [Function], 
       augment: [Function], 
       toString: [Function], 
       register: [Function], 
       __super__: 
       { bind: [Function], 
       unbind: [Function], 
       trigger: [Function], 
       tagName: 'div', 
       '$': [Function], 
       initialize: [Function], 
       render: [Function], 
       remove: [Function], 
       make: [Function], 
       delegateEvents: [Function], 
       _configure: [Function], 
       _ensureElement: [Function], 
       html: [Function], 
       toString: [Function], 
       template: [Function] } } 
     */ 
     var myview = new view(); 
     this.send(myview, {collection: projects}); 
    } 
}); 

回答

0

您需要在您設置使用jQuery el:

var view = Backbone.View.extend({ 
     el: $('body'), 
     render: function() { 
      $(this.el).empty().append(templates.Project({content : 'this works!' })); 
      return this; 
     } 
    }); 
var myview = new view(); 

檢出this answer欲知更多信息。

+0

啊。我想我在設置el的時候並沒有選擇它。萬分感謝。 – thewildpendulum 2012-01-31 22:13:06

+0

或'tagName:'body''也許? – Tjorriemorrie 2012-02-01 06:06:59

0

要不要試試

new view 

,而不是

new view(); 

或者,也許嘗試在模型傳遞:

new view({}); 
+0

沒有骰子。它仍然給我同樣的錯誤信息。 – thewildpendulum 2012-01-31 18:46:09

相關問題