2013-05-22 3 views
0

다른보기에서 단일 모델을 수정하려면 어떻게해야합니까?BackboneJS : 다른보기에서 단일 모델 수정

의사 코드 :

var myModel = Backbone.Model.extend({url: 'rest'}); 
var myCollection = Backbone.Model.extend({}); 

var myView1 = Backbone.Views.extend({ 
    initialize: function() { 
     // sets a data in model so it can be use by second and third view 
     myModel().fetch(); 
    }, 
    events: { 
     'event': 'callSecondView', 
     'event': 'callThirdView' 
    }, 
    callSecondView: function() { 
    // second view will use the current records in myModel that 
    // was created during initialization or from the modification 
    // of 1st or third view 
    new myView2({model: myModel}) 
    }, 
    callThirdView: function() { 
    // third view will use the current records in myModel that 
    // was created during initialization 
    // of 1st or third view 
    new myView3({model: myModel}) 
    } 
}); 

var myView2 = Backbone.Views.extend({ 
    events: { 
     'event': 'modifyMyModel' 
    }, 
    modifyMyModel: function() { 
     this.model.set(etc) 
     // Modifying model here should affect the 1st and 3rd view's model 
    } 
}); 

var myView3 = Backbone.Views.extend({ 
    events: { 
     'event': 'modifyMyModel' 
    }, 
    modifyMyModel: function() { 
     this.model.set(etc) 
     // Modifying model here should affect the 1st and 2nd view's model 
    } 
}); 

이 가능합니까? 아니면 하나의보기에서 3 개의 모든보기를 결합해야합니까? (괴물보기로 표시됩니다)

답변

2

매우 못 생겼습니다. 먼저 모델을 가져온 다음 원하는만큼보기를 생성하지 마십시오. :

var MyModel = Backbone.Model.extend({url: '/api/data'}); 

var View1 = Backbone.View.extend({}); 

var View2 = Backbone.View.extend({}); 

var View3 = Backbone.View.extend({}); 
var myModel = new MyModel(); 
myModel.fetch({ 
success: function(){ 
    window.myView1 = new View1({model:myModel}); 
    window.myView2 = new View1({model:myModel}); 
    window.myView3 = new View1({model:myModel}); 
} 
}); 

// in console 
myView1.model == myModel 
true