2013-07-19 101 views
2

我有一個模型,如:如何通知從模型視圖更改值在骨幹

var resultEntityModel = Backbone.RelationalModel.extend({ 
     defaults : { 
      name : "", 
      bgOccurence : "", 
      fgOccurence : "", 
      bgOccurenceCount : "", 
      fgOccurenceCount : "", 
     }, 
     initialize : function() { 

      var bgOccurence = this.set("bgOccurence", bgOccurence);  
      var fgOccurence = this.set("fgOccurence", fgOccurence); 
      if((Math.abs((bgOccurence-fgOccurence))) >= 10){   
       // send information to view that background should be red 
      }else{ 
       // send information to view that background should be white 
      } 
     }, 
}); 

和一個視圖:

var resultEntityView = Marionette.CompositeView.extend({ 
     tagName : "tr", 
     template : ResultEntityPanel, 
     initialize: function() { 
       this.model.on(...) // it should take information from model 
            //and change css values according to this information 
     }, 

    }); 

我如何可以從模型的信息,並將其發送查看以便更改.css文件中的某些值?我知道這個方法,但我不知道該怎麼做。

+0

'this.model.on( '變',函數() {console.log(「hidyho」);});' - 但是對於更改.css文件這將是一個不行,您可以使用jQuery來更改某些東西的CSS。 –

回答

1

您希望視圖聽取模型上的更改。請參閱Backbone listenTo

未經測試的代碼,但這樣的事情應該這樣做:

型號:

var resultEntityModel = Backbone.RelationalModel.extend({ 
     defaults : { 
      name : "", 
      bgOccurence : "", 
      fgOccurence : "", 
      bgOccurenceCount : "", 
      fgOccurenceCount : "", 
     }, 
     initialize : function() { 
      this.set("bgOccurence", bgOccurence);  
      this.set("fgOccurence", fgOccurence); 
     } 
    }); 

觀點:

var resultEntityView = Marionette.CompositeView.extend({ 
     tagName : "tr", 
     template : ResultEntityPanel, 
     initialize: function() { 
      this.listenTo(this.model, "change:bgOccurence", this.setBg); 
      this.listenTo(this.model, "change:fgOccurence", this.setBg); 
     }, 
     setBg: function(model, value) { 
      var bgOccurence = model.get('bgOccurence'), 
       fgOccurence = model.get('fgOccurence'); 
      if ((Math.abs((bgOccurence-fgOccurence))) >= 10) { 
       $('body').css({'background-color':'red'}) 
      } else { 
       $('body').css({'background-color':'white'}) 
      } 
     } 
    });