Search code examples
javascriptbackbone.jsunderscore.jslisten

How to listeTo a particular options in backbone.js?


I have view which is initialized as below

app.OfferMenuView = Backbone.View.extend({
    events: { },
    initialize: function(options) { 
        this.options = options || {};
    }
});

I create an instance of it along with an option -

this.offermenu = new app.OfferMenuView({offer_heading:-1});

Now I want to bind a listenTo to listen to the change in the value of the options.offer_heading .

I have tried with writing this:

this.offermenu.listenTo(this.offermenu,'change:options',this.offermenu.render);

But this does not go to render function on change of offer_heading. What is the correct way of binding a listenTo for change in an options value ?

Thanks in advance.


Solution

  • example:

    app.OfferMenuView = Backbone.View.extend({
        events: { },
        initialize: function() {   
            this.listenTo(this.model,'change:options', this.render);
        }
    });
    
    app.Model = Backbone.Model.extend({ 
        defaults: {
            //options
            foo: 'bar',
            //....
        }
    });
    
    var newModel = new app.Model();
    
    this.offermenu = new app.OfferMenuView({model: newModel});
    

    view -> listenTo (model)

    you can see this page: http://backbonejs.org/#Events-listenTo

    view.listenTo(model, 'change', view.render);