el_pup_le
el_pup_le

Reputation: 12179

Why is el needed as view parameter

Since you can specify a view's el as one of it's properties why does it still need to be passed as a parameter to work?

Eg.

var DetailsView = Backbone.View.extend({    
   el: $(".details")
});

As parameter:

var DetailsView = new DetailsView({ el: $(".details") });

Upvotes: 0

Views: 34

Answers (1)

quinnirill
quinnirill

Reputation: 834

It doesn't need to be specified. If you already at that point have an element that matches the selector, this should work:

$('body').append($("<div class='details'></div>"));

var DetailsView = Backbone.View.extend({    
   el: $(".details")
});

console.log(new DetailsView().el.className); // details

I suspect that your element is not in DOM when you define the view.

How Backbone.View works is that it checks whether you specified el as an attribute and if not, it checks if el was specified in the prototype, and if not, it uses the tagName, className and attributes properties on the prototype to create a new element for the view.

Upvotes: 2

Related Questions