2014-02-09 4 views
3

다음은 컬렉션에서 올바른 데이터를 가져 오기 위해 초기화 될 때 모듈 이름을 받아야하는 뷰의 제 초기화 함수입니다.collectionTo에 대한 listenTo는 콜렉션을 가져온 후에 호출되지 않습니다.

문제는 다음과 같습니다 컬렉션이 인출 된 후

  1. Listento이 렌더링() 메소드로 리디렉션하지 않습니다, 또한 나에게 콘솔

TypeError: e is undefined

에 오류가 있습니다 아래 코드로 실수 한 것은 무엇입니까 ??

initialize: function() {   

    var that = this; 
     if(this.options.module === 'questions'){     

      require([ 
       'app/collections/questions' 
      ], function(QuestionsCollection){     
       var moduleCollection = new QuestionsCollection(); 
       that.collection = moduleCollection; 
       moduleCollection.fetch({ 
        reset: true, 
        success: function(){}, 
        error: function(){} 
        });                   
      });     

     } 

     this.listenTo(this.collection, 'reset', this.render); 
     this.listenTo(Backbone, 'close:Home', this.close); 
    }, 

답변

1

당신의 문제가 riquire.js입니다. 따라서 this.listenTo(this.collection, 'reset', this.render);을 호출하면이 마지막 행이 실행될 때 require([...을 호출하면 아직 이로드되지 않고 callback이 아직 호출되지 않으므로 this.collection은 정의되지 않습니다.

http://jsfiddle.net/ZZuGC/1/을 예로 들자면 console.log('require'); & console.log('listenTo');이 콘솔에 인쇄됩니다. 나는 예를 http://jsfiddle.net/ZZuGC/2/

+0

나는 당신의 분석에 문제가 있다고 확신하지만 이런 상황에서 어떻게 할 것을 제안 하는가? 나는이 해결책을 사용하여 요청에 의해서만 서버로부터로드 된 파일의 수를 줄였다. – ahmedsaber111

4

본인은이 모듈이 폐쇄에 요구하는 곳이 범위의 문제 믿습니다. 다음보십시오 : 당신이 가로드 된 후 callback에 대한 호출이 비동기 적으로 만들어 require([/*jsFile*/], callback);을 쓸 때

initialize: function() {   

var that = this; 
    if(this.options.module === 'questions'){     

     require([ 
      'app/collections/questions' 
     ], function(QuestionsCollection){     
      var moduleCollection = new QuestionsCollection(); 
      moduleCollection.fetch({ 
       reset: true, 
       success: function(){}, 
       error: function(){} 
       }); 
      that.listenTo(moduleCollection, 'reset', that.render);              
     });     
    } 

    this.listenTo(Backbone, 'close:Home', this.close); 
}, 
+0

감사합니다, 나는 그것이 범위의 문제가 될 수 있다는 생각에 대한 많은과를 업데이트 한

initialize: function() { var that = this; if(this.options.module === 'questions'){ require([ 'app/collections/questions' ], function(QuestionsCollection){ var moduleCollection = new QuestionsCollection(); that.collection = moduleCollection; // here is my suggestion that.listenTo(that.collection, 'reset', that.render); moduleCollection.fetch({ reset: true, success: function(){}, error: function(){} }); }); } this.listenTo(Backbone, 'close:Home', this.close); }, 

:


나는 다음과 같은 문제의 해결책이 생각 당신이 방금 언급 한 것을 포함하여 많은 해결책을했지만 그것은 문제가 아니라고 생각합니다. Rida의 답이 그것을 해결하기 위해 더 가깝다고 생각합니까? – ahmedsaber111