2016-10-20 2 views
1

어쨌든 listenTo 메소드없이 다른 뷰에 백본 콜렉션을 보내어 배열이나 다른 것과 같이 보내도록 할 수 있습니까?컬렉션을 다른보기로 보내는 방법은 무엇입니까?

초기화 기능에서 가져 오기를 수행하고 컬렉션을 내 배열에 넣는 것은 나쁘지 않습니까?

this.userModels = []; 

this.collectionUser = new app.types.Users(); 
this.collectionUser.fetch(); 

this.userModels.push(this.collectionUser); 

배열처럼 보내려고 임하지만 때로는 내 웹 페이지를 새로 고침에서이

this.options child { 
    length: 15, 
    models: Array[15], 
    _byId: Object, 
    _listenId: "l4", 
    _events: Object… 
} 

를 받고 메신저 언젠가 0 값

this.options child { 
    length: 0, 
    models: Array[0], 
    _byId: Object, 
    _listenId: "l4", 
    _events: Object… 
} 

그래서 난 내를 보내 싶어

로 점점 가능한 경우 listenTo 메서드가없는 컬렉션.

먼저보기 :

app.types.FirstView = Backbone.View.extend({ 

    initialize: function() { 
    this.collectionUser = new app.types.Users(); 
    this.collectionUser.fetch(); 
    }; 

    sendCollection: function() { 
    var secondView = new app.types.SecondView({ 
     collection: this.collectionUser 
    }); 
    } 
}); 

두 번째보기 :

app.types.SecondView = Backbone.View.extend({ 

    initialize: function(options) { 
    this.collection = options.collection; 

    // so i want to get this.collectionUser here without listenTo 
    // method and without fetch here is that possible ? I said 
    // sometimes i get it sometimes not when i refersh my web page 
    // when i render it first time. 
    }; 

}); 

답변

2

예는 JS로 모든 것을주고 그보기를 초기화 할 수 있습니다. 보기에서 이와 같이 초기화 함수를 선언해야합니다.

var someView = Backbone.View.extend({ 
    template: _.template('some html file'), 
    initialize: function(options){ 
     this.collection = options.collection 
    }, 
    events: { 
     'click .someclass': 'doSomthing', 
     'click #someId': 'doSomthingElse' 
    }, 
    doSomthing: function(event){ 
     event.preventDefault(); 
     var that = this; 
     that.collection.fetch({ 
      success: function(){ 
       that.$('element').append(that.collection); 
      } 
     }); 
    }, 
    render: function(){ 
     var that = this; 
     that.$el.html(that.template); 
     return this; 
    } 
}); 

그리고 뷰의 새 인스턴스를 만들 때 컬렉션을 인수로 전달해야합니다.

this.collectionUser = new app.types.Users(); 
this.view = new someView({collection: this.collectionUser}); 

이것은 그것을

+0

나는 그것을했지만 다시보기 길이에 널 (null) '아이 점점 메신저 {길이 : 0, 모델 : 배열 [0], _byId : 개체}'. – lorenos

+1

그럴 경우 콜렉션 인수와 뷰 콜렉션에 전달해야한다. colection.fetch() –

+0

나는 지금 대답을 바꿀 것이다 –

관련 문제