2012-04-30 24 views
0

使用骨幹JS庫來構建頁面。 我有一個集合,視圖和路由器。 目標是列出頁面中所有集合的條目。Backbone Collection.fetch()奇怪的行爲

收集

window.Artifact = Backbone.Model.extend({ 
    urlRoot:"/rest/artifacts", 

    initialize:function(){ 
     this.artifacts = new ArtifactCollection(); 
    } 
}); 

window.ArtifactCollection = Backbone.Collection.extend({ 

    model:Artifact, 
    url:"/rest/artifacts" 

}); 

var artifacts = new ArtifactCollection; 

視圖

window.findView = Backbone.View.extend({ 

    initialize:function() { 
     console.log('Initializing Find View'); 
     this.template = _.template(tpl.get('find')); 
    }, 

    render:function (eventName) { 

    var data = { 'data' : artifacts.models }; 
      $(this.el).html(this.template(data)); 
      return this; 
    } 
}); 

路由器程序

var AppRouter = Backbone.Router.extend({ 

routes:{ 
    "":"home", 
    "contact":"contact", 
    "get":"get", 
    "find":"find", 
    "requests/:id":"requests" 
}, 

find:function(){ 

    artifacts.fetch({ 
     success:function(){ 
      this.findView = new findView(); 
      this.findView.render(); 
      $('#content').html(this.findView.el); 
     } 
    }); 
}, 

的問題是,它的作品大部分時間,但有時不工作。 它會拋出以下異常

Uncaught TypeError: object is not a function 
artifacts.fetch.successmain.js:53 
_.extend.fetch.options.successbackbone-0.9.2.js:760 
jQuery.Callbacks.firejquery.js:1032 
jQuery.Callbacks.self.fireWithjquery.js:1150 
donejquery.js:7385 
jQuery.ajaxTransport.send.callback 

以前有人看到過這種類型的問題嗎?

+2

等等,工件模型有一個工件的集合? – asawyer

+0

ArtifactCollection是一個工件集合 – Nambi

+0

感謝您的指點。我應該刪除它。 – Nambi

回答

0

I renamed the this.findView to this.find in this snippet of code in the Router. artifacts.fetch({ success:function(){ this.find = new findView(); this.find.render(); $('#content').html(this.find.el); } }); Now everything works. Can someone explain what is the significance of this change?

我還不能肯定,但我猜你success功能,thiswindow的引用,當你有this.findView = new findView();,你覆蓋window.findView,造成「遺漏的類型錯誤:對象不是一個函數「,當success函數再次運行並試圖呼叫new findView()