0

我正在安慰高度,当它发生变化时,我还将更改事件绑定到元素。我仍然没有在这里得到控制台输出......

我对骨干很陌生,有人纠正我的错误吗?

var Person = Backbone.Model.extend({
                initialize:function(){
                    var getHeight = prompt('provide your height');
                    this.set({height:getHeight}); //i am changing the height...
                    this.bind('change:height',function(){
                        console.log(this.get('height'));  //i am not getting any console here...  
                    })
                },
                defaults:{
                    name:'new Name',
                    height:'unknown'
                }

            });

            var person = new Person();
4

1 回答 1

1

如果要监听事件,设置绑定的值:

var Person = Backbone.Model.extend({
    initialize:function(){
        // bind is deprecated
        this.on('change:height',function(){
            console.log(this.get('height'));
        });

        var getHeight = prompt('provide your height');
        this.set({height:getHeight});
    },
    defaults:{
        name:'new Name',
        height:'unknown'
    }
});
于 2012-12-11T13:21:35.230 回答