javascriptbackbone.jsrequirejsunderscore.jsrequirejs-text

(8 out of range 6) Underscore.js Templates


I am using Backbone.js and underscore.js with Requirejs. However when i try to load my view template, it gives me (8 out of range 6) error in Underscore.js line 8. Please tell me what i am doing wrong.

Here is my code:

var imageView = new ImageView({model: item});


define(['jquery','underscore','backbone','imageview','text!../templates/template_image.html'], 
function($, _, Backbone, ImageView, template){
        var ImageView = Backbone.View.extend({
            initialize: function(){
                this.showImageTemplate = _.template(template);              
            },
            render: function(){
                var html = this.showImageTemplate(this.model);
                this.$el.html(html);
                return this;
            }
        });
    return ImageView;
});

And my Template file:

<img id="frameImg" src="<%= DocumentPath %>/<%= DocumentName %>" alt="image" title="image"/>

Solution

  • You're passing the raw Backbone.Model object as data to your template, so you're working with something like

    {       
        _changing: false,
        _pending: false,
        _previousAttributes: {}
        attributes: {
            DocumentPath: "", 
            DocumentName: ""
        }
        ...
    }
    

    You probably only want the attributes, which you can obtain via model.toJSON for example. Try :

    var html = this.showImageTemplate(this.model.toJSON());