0

I am learning backbone.js. I found the following code from backbone.js documentation.

 //cretaing class name as Sidebar with extends Backbone.Model class  
 var Sidebar = Backbone.Model.extend({
                promptColor:function() {
                    var cssColor = prompt("Please enter a CSS color:");
                    this.set({color1: cssColor});
                }
        });
  //cretaing object for Sidebar class
        var sidebarObject = new Sidebar();
  //following statement not understand
        sidebarObject.on("change:color1", function(model, color23) {
            $('#body1').css({background: color23})
        });

What I understand :

whenever attribute color1 will change automatically model('Slidebar') triggers sidebarObject.on().

What I didn't understand :

  1. we are passing 2 arguments names are model,color23 to second argument of the sidebarObject.on(). I didn't understand anything about that arguments.

2.As per my understanding model is Sidebar in my example.Is it right.

can anyone help me.

Thanks.

4

1 回答 1

2

是的,您的第二个假设是正确的,在上面的示例中,侧边栏是模型。

有 2 个参数被传递给回调函数:

  1. 最初发生事件的模型
  2. 与事件一起传递的值

请注意,sidebarObject仅侦听“颜色”更改事件。当您更改模型的特定属性时会生成此类事件:

this.set({color1: cssColor});

现在,如果您将侦听器更改为:

sidebarObject.on("change:taste", function(model, color23) {
    $('#body1').css({background: color23})
});

被调用时它不会再着火了this.set({color: cssColor});,因为它会听到“味道”的变化:

this.set({taste: 'good'});

我把上面的例子分成了一个小提琴,所以你可以玩它:http: //jsfiddle.net/CtzsR/1/

如果您对整个事情不熟悉 http://backbonetutorials.com/what-is-a-model/是一个值得一看的网站。

我希望现在它更清楚了。

于 2013-10-17T12:43:57.740 回答