backbone.js A View Bound to Existing HTML


Example

Assuming this HTML in the page:

<body>
    <div id="myPage">
    </div>
</body>

A view can be bound to it with:

var MyPageView = Backbone.View.extend( {
    "el": "#myPage",
    "template": _.template( "<p>This is my page.</p>" ),

    "initialize": function(){
        this.render();
    },

    "render": function(){
        this.$el.html( this.template() );
    }
} );

new MyPageView();

The HTML in the browser will now show:

<body>
    <div id="myPage">
        <p>This is my page.</p>
    </div>
</body>