3gwebtrain
3gwebtrain

Reputation: 15303

Backbone - on change not consoling.. what is wrong?

I am consoling the height, while it get changed, as well i am binding the change event to the element. still i am not getting the console output here...

i am very new to backbone, any one correct my mistake?

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

Upvotes: 0

Views: 121

Answers (1)

nikoshr
nikoshr

Reputation: 33344

Set the value after the binding if you want to monitor the event:

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'
    }
});

Upvotes: 1

Related Questions