0

我创建了两个模型名称是'Sidebar','listenTestClass'。我在模型中创建了color1属性,同样我在模型中创建了属性。SidebarclistenTestClass

//listenTestClass model
var listenTestClass=Backbone.Model.extend(
                {
                    test:function(value){
                        this.set({c:value});
                    }                       
                }
            );
        var listenTestClassObject=new listenTestClass();
 //sidebar model
 var Sidebar = Backbone.Model.extend({
                promptColor:function() {
                    var cssColor = prompt("Please enter a CSS color:");
                    this.set({color1: cssColor});
                }
        });
var sidebar = new Sidebar();
 //if color1 attribte change,it triggers the following code
 sidebar.on("change:color1", function(model, color2) {
            $('#body1').css({background: color2})
        });
//assigning white color to my body tag.
sidebar.set({color1:'white'}); 
//assigning some random value to "c" attribute
listenTestClassObject.test("hi");

对象sidebar侦听属性“C”。这意味着每当属性“C”值发生变化时,我都想应用 is 的背景颜色green。为此,我使用了该listenTo事件,但它不起作用。

sidebar.listenTo(listenTestClassObject,"change:c",function(model,c1){sidebar.set({color1:'green'})});

谁能帮我。

4

0 回答 0