Reputation: 3601
How set Parent element for backbone view ?
var TodosView = Backbone.View.extend({
tagName: 'p', // required, but defaults to 'div' if not set
className: 'container', // optional, you can assign multiple classes to
// this property like so: 'container homepage'
id: 'todos', // optional
initialize: function(){
// debugger
this.$el.html("bamboo4a")
$("body").append(this.$el);
}
});
var todosView = new TodosView();
I do not want to write $("body").append
Upvotes: 3
Views: 406
Reputation: 7295
For the main view you can set the view element when creating the view object by passing the options to its constructor (namely the option el
).
var MyView = Backbone.View.extend({
template: '<p>Hello World!</p>',
render: function() {
this.$el.html(this.template);
}
});
new MyView({
el: 'body' // or el: '#content' and so on
}).render();
Demo with more detailed example of the application
Upvotes: 2