Search code examples
backbone.jsbackbone-views

which DOM element binded to my Backbone view?


I am reading codes:

module.exports = Backbone.View.extend({
  tagName: 'div',
  events: {
    'keydown': '_enter',
  }
  ...

The tagName is "div", How can I know which div this is?

Thanks


Solution

  • From the fine manual:

    el view.el
    [...]
    this.el can be resolved from a DOM selector string or an Element; otherwise it will be created from the view's tagName, className, id and attributes properties.

    So el is resolved from a selector string (i.e. looked up on the page) or created from tagName and friends. If you don't bind to a specific existing element then Backbone will create one for you using tagName, className, ...

    If you just have tagName: 'div' then the <div> your view is bound to is simply the view's el and you are responsible for putting that el somewhere on the page. Hence the common pattern of:

    $(some_container).append(view.render().el);