未在JavaScript中定义上下文
因此,我在这里提到了最初的问题,在做了大量的谷歌搜索之后,我找到了一个我认为可行的解决方案,就是这个 然而,我用以下方式实现了我的未在JavaScript中定义上下文,javascript,jquery,html,templates,handlebars.js,Javascript,Jquery,Html,Templates,Handlebars.js,因此,我在这里提到了最初的问题,在做了大量的谷歌搜索之后,我找到了一个我认为可行的解决方案,就是这个 然而,我用以下方式实现了我的 var EmployeeView = function(employee){ this.render = function(){ $('body').append(this.el); // inside the new div, put the Handlebars template, with the data from the emplo
var EmployeeView = function(employee){
this.render = function(){
$('body').append(this.el);
// inside the new div, put the Handlebars template, with the data from the employee
this.el.html(EmployeeView.template(employee)).ready( function() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
});
return this;
};
this.initialize = function(){
this.el=$("</div>");
window.localStorage.removeItem('paramId');
console.log('removed');
window.localStorage.setItem('paramId',employee);
console.log('added ' + employee.id);
};
this.initialize();
}
EmployeeView.template = Handlebars.compile($("#employee-tpl").html());
在这方面:
$("employee-tpl").append(HTMLtemplate(context))
您正在传递一个undefined
函数和一个undefined
变量(context
)。
context
应该包含您在HandlebarJS模板中定义的要替换的id
参数
<a href="#map/{{this.id}}">See on map</a>
我认为您必须将
employee
传递给模板,因为我在模板中看到了first\u name
和last\u name
。这意味着变量context
未定义:是否定义了HTMLtemplate(context)
函数?否则它应该是EmployeeView.template
,我猜是在那个地方。但引用它看起来不像HTMLTemplate,而且上下文
是额外定义的吗?请纠正我:(我可以在页面的代码片段中看到它们的定义。你好,Marco,我已经如上所述更新了我的代码,但是,它现在抛出另一个错误Uncaught ReferenceError:employee is not defined
遵照您的建议@marcocl,它现在抛出Uncaught TypeError:Cannot read属性'offsetWidth'of null
,我理解his是由于调用此函数时未呈现div造成的\n它找不到要附加模板的元素:我已更新了答案。;)您好,Marco,非常感谢您的回答,但是它仍然抛出相同的错误。我已更新了上述问题
<a href="#map/{{this.id}}">See on map</a>
var EmployeeView = function(employee){
this.render = function(){
$('body').append(this.el);
// inside the new div, put the Handlebars template, with the data from the employee
this.el.html(EmployeeView.template(employee))).ready( function() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
});
return this;
};
this.initialize = function(){
this.el=.$("</div>");
window.localStorage.removeItem('paramId');
console.log('removed');
window.localStorage.setItem('paramId',employee);
console.log('added ' + employee.id);
};
this.initialize();
}
EmployeeView.template = Handlebars.compile($("#employee-tpl").html());