poseid
poseid

Reputation: 7156

Why is el undefined and $el defined for this Backbone View?

I am confused about why '''el''' is undefined here, and $el is defined.

undefined el

The background is en experiment with CoffeeScript as follows:

class FastTodo.Views.AddTodoItem extends Backbone.View

  template: JST['todo_items/add_item']

  el: $('#main')

  render: ->
    console.log("render")
    console.log($("#main"))
    console.log(@el)
    console.log(@)
    $(@el).html @template

  initialize: ->
    @render()

How can I render the view in this case?

Upvotes: 4

Views: 6332

Answers (1)

iMoses
iMoses

Reputation: 4348

Try rewriting the element declaration to el: '#main'

I think that should work well for you.

By the way, according to your console log the jQuery element ($el) is empty as well. You must be declaring the view before the markup has been fully loaded. By giving el the selector for the elements you make sure it will be fetched only when the document is ready (loaded).

Upvotes: 5

Related Questions