Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未在JavaScript中定义上下文_Javascript_Jquery_Html_Templates_Handlebars.js - Fatal编程技术网

未在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());