thgie thgie - 1 year ago 64
Javascript Question

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

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download