sly_Chandan
sly_Chandan

Reputation: 3515

$el is undefined in my view. Why?

var divElement = Backbone.View.extend({        
        initialize: function () {
            this.render();
        },          
        render: function () {                        
            $el.append("<ul id='ulList'></ul>"),
            wines.each(function (model) {
                $("#ulList").append("<li>" + model.winename + "</li>");
            });
            return this;
        }
    });

When I instantiate my view I get $el as undefined... why so? By default el should be a div

Upvotes: 0

Views: 779

Answers (2)

Griffin M
Griffin M

Reputation: 461

This is an issue with scope, in the scope of your function JS does not know where to find the $el variable, so it thinks it is undefined because no where in the function is it explicitly defined. However, it is defined on the "this" object, so this.$el will return the element. If you write you javascript in something called strict mode, small mistakes like this will get picked up automatically.

Upvotes: 1

billjamesdev
billjamesdev

Reputation: 14642

You need to add "this":

    this.$el.append("<ul id='ulList'></ul>"),

see here: http://backbonejs.org/#View-render

Seems append works just as well as html() for this.

Upvotes: 1

Related Questions