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 ファイルの値を変更するために、モデルから情報を取得してビューに送信するにはどうすればよいですか? 方法は知っているのですが、やり方がわかりません。

4

1 に答える 1

1

ビューがモデルの変更をリッスンするようにします。バックボーン 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'})
            }
        }
    });
于 2013-07-19T07:39:54.767 に答える