Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parent element for backbone view?

Tags:

backbone.js

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

like image 377
Michael Phelps Avatar asked Apr 20 '26 22:04

Michael Phelps


1 Answers

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

like image 187
Eugene Naydenov Avatar answered Apr 22 '26 22:04

Eugene Naydenov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!