骨干 - 改变不安慰..有什么不对?

时间:2012-12-11 13:10:23

标签: backbone.js backbone-model

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

我对骨干很新,任何人都纠正了我的错误?

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();

1 个答案:

答案 0 :(得分: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'
    }
});