Search code examples
javascriptbackbone.js

backbone.js / access view from model


How can I access the view from a model in backbone.js.

I would love to re-render the view on model.change().


Solution

  • Adding views to the model's attribute is a no-no.

    Why would you need to access the view from model on its change?

    In your view, simply bind:

    this.model.bind('change', this.modelChanged, this) // (event, function, context)
    

    and from now on, when your model changes, your view's modelChanged method will be called automatically.


    in version >0.9, the proper syntax will be like this in the view.

    this.model.on('change', this.modelChanged, this) // (event, function, context)