1
votes
  • My first approach was to have each View initialize (new) its own Model, but as the project grows it is getting difficult to maintain as more views get added.

  • Another way could be to have the App level View do the job.

Since Backbone does not provide a Controller class. What is the best practice to connect views and models in a medium size project in Backbone?

I know this is yet another Backbone Controller question from a MVC newbie! And that Backbone is not strict MVC. I only found some answers to this question in SO that were outdated and mixed Router and Controller concepts.

1
Can you elaborate on the difficulties mentioned in your first bullet? - Ken Browning
This is due mostly to some views (and models) being nested, it is increasingly difficult to keep track of which view initializes which model. Quickly it becomes a dependency nightmare. - miguelr

1 Answers

2
votes

Just connect them when you create your view by specifying the model in the configuration object you pass to the view constructor:

var MyModel = Backbone.Model.extend();
var myModel = new MyModel();
var MyView = Backbone.View.extend();
var myView = new MyView({model: myModel});