Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 560
thgie Avatar asked Apr 12 '11 07:04

thgie


1 Answers

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)
like image 96
pawlik Avatar answered Sep 28 '22 12:09

pawlik