2012-04-30 3 views
0

백본 JS 라이브러리를 사용하여 페이지를 빌드합니다. 컬렉션,보기 및 라우터가 있습니다. 그리고 목표는 페이지에서 컬렉션의 모든 항목을 나열하는 것입니다.백본 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();을했을 때, 당신은 "catch되지 않은 형식 오류의 결과로, window.findView을 덮어했다 : 객체 함수가 아닙니다 "success 함수를 다시 실행하고 new findView() 호출하려고합니다.