Jamie Anderson
Jamie Anderson

Reputation: 119

backbone.js tag name has no effect after declared

I think I've declared tagName correctly? and it's placed correctly under render method, but somehow the tag is div not h1.

https://jsfiddle.net/b01vtfnh/

        var Person = Backbone.Model.extend({
            defaults:{
                name:"",
                age:20,
                job:'jobless'
            }
        });

        var person1 = new Person({
            name:"Sony",
            age: 30,
            job: "Teacher"
        });

        var PersonView = Backbone.View.extend({
            initialize: function(){
                this.render();
            },
            render: function(){
                tagName:'h1'; //this line won't work

                var li_person = this.model.get('name') + ', ' + this.model.get('age') + 
                ' (' + this.model.get('job') + ')';

                this.$el.html(li_person)
                return this;
            }
        });

        var person_view = new PersonView({model:person1});
        $('body').html(person_view.el)

Upvotes: 0

Views: 60

Answers (1)

rockerest
rockerest

Reputation: 10518

tagName is a property of the view, not - somehow - of the render function. This should throw a syntax error as written. The correct structure would be:

initialize: function(){
    this.render();
},
tagName: 'h1',
render: function(){
...

I'm leaving the original so the comments make sense, but it turns out that function(){ thing: "stuff"; } is actually valid syntax. Regardless, in the context and the way it's used, it is incorrect for it to be positioned there.

Upvotes: 3

Related Questions