boburShox
boburShox

Reputation: 2630

model and collection to one view in Backbone

Being new to Backbone.js, I want to ask one general and simple question. In my app I need to create a new view with giving a collection and a model in it.

var myView = new SomeView({
    model: myModel,
    collection: myCollection,
    el:$('#main_content')});
}});

Interestingly, I got my stuff worked. What do you think if it suits with the concept of backbone, or what?

Thank you..

Upvotes: 1

Views: 238

Answers (1)

jevakallio
jevakallio

Reputation: 35970

There's no reason you can't do that, but it's a bit confusing. Normally you can infer the type of the model or collection from the name of the view. For example, the collection of EmployeeListView will probably be a EmployeeCollection, and the model or CompanyView will most likely be a Company.

But when you have both a model and a collection, the meaning is not clear. In such case I would name the parameters more meaningfully. The model and collection properties are just conventions - Backbone views don't really need either.

Let's say you have a view that displays a Company and it's Employees:

var CompanyEmployeesView = Backbone.View.extend({
  initialize: function(options) {
    this.company = options.company;
    this.employees = options.employees;
  }
});

var view = new CompanyEmployeesView({
  company: company,
  employees: employeesCollection
});

Upvotes: 2

Related Questions