Michael Phelps
Michael Phelps

Reputation: 3601

Parent element for backbone view?

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

Answers (1)

Eugene Naydenov
Eugene Naydenov

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

Documentation

Demo

Demo with more detailed example of the application

Upvotes: 2

Related Questions