I'm trying to render some HTML using the underscore template function. The problem is I'm getting Uncaught ReferenceError: data is not defined when I load the script locally on my machine.
It seems to work on jsfiddle, but not in my environment and I have no idea why. Here is the working js fiddle.
and here is my code:
define([
'jquery',
'backbone',
'underscore',
'moment',
'bootstrap-modal',
'domReady!'
], function (
$,
Backbone,
_,
moment
) {
'use strict';
/**
* Represents a booking modal view
*/
return Backbone.View.extend({
defaults: {
modalHeadingHtml: '<div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title"><%= data.title %></h4> </div>',
modalBodyHtml: null,
modalFooterHtml: null
},
template: _.template('<div class="modal-dialog"><div class="modal-content"><%= data.content %></div></div>'),
className: 'wl-car-book-modal modal wl-modal fade',
/**
* Initializes booking modal view.
*/
initialize: function (options) {
this.options = _.extend({}, this.defaults, this.options);
$('body').append(this.render().el);
this.$el.modal()
var data = {data: { title: 'fooo'}};
var tpl = _.template(this.options.modalHeadingHtml);
this.$('.modal-content').append(tpl(data));
this.open();
},
render: function() {
var wrapperHtml = this.template({data: {content: 'test'}});
this.$el.html(wrapperHtml);
return this;
},
open: function() {
this.$el.modal('show');
},
close: function() {
this.$el.modal('hide');
}
});
});
Yes there are some differences, but mainly it's just about the apparent undefined variable in the initialize function.
Any help would be greatly appreciated, thanks.
this.$el.modal()in the fiddle, it's a lot different. Also, how are you instantiating the view ? Have you verified that the view's el hasmodalfunction. - coding_idiot