user3615258
user3615258

Reputation: 5

Uncaught ReferenceError: template is not defined in backbone js

want to make load external template from html in backbone view person and display the values from model person but get error in console say that template not defined my code is right so what problem

    <script type="text/template" id="personTemplate" >
    <strong><%= name %></strong> 
        <strong><%= age %></strong>
        <strong><%= occupation %></strong>
    </script>  


var Person = Backbone.Model.extend({
        initialize: function () {
            this.on("invalid", function (model, error) {
                alert(error);
            });
        },
        defaults: {
            name: 'Guest User',
            age: 23,
            occupation: 'worker'
        }
    });
    var PersonView = Backbone.View.extend({
        template: "#personTemplate",
        initialize: function () {
            alert("view success");
            this.render();
        },

        render: function () {
            template = _.template($(template).html())
            this.$el.html(template, this.model.toJSON());

        }
    });
    $(document).ready(function () {

        var person = new Person
        var personView = new PersonView( { model:person });
     personView.el;
    $(document.body).html(personView.el);  
    });

Upvotes: 0

Views: 2504

Answers (1)

David Sulc
David Sulc

Reputation: 25994

Try with

template = _.template($(this.template).html());

in your render function.

Upvotes: 2

Related Questions