2012-04-30 20 views
0

バックボーンJSライブラリを使用してページを構築する。 コレクション、ビュー、およびルーターがあります。 そして目標は、ページ内のコレクションのすべてのエントリを一覧表示することです。Backbone Collection.fetch()strange behavior

コレクション

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();を持っていたとき、あなたは「キャッチされない例外TypeErrorが得られ、window.findViewを上書きしました:オブジェクト関数ではありません "success関数が再度実行され、new findView()を呼び出そうとします。