javascript - 上下文未定义 JavaScript

标签 javascript jquery html templates handlebars.js

因此,引用我在这里遇到的原始问题 Google Maps Javascript V3 Uncaught TypeError: Cannot read property 'offsetWidth' of null经过大量谷歌搜索后,我找到了一个我认为可行的解决方案,这就是 How to append multiple Google Maps areas to divs using Handlebars

但是,我通过以下方式实现了我的

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

不确定这是否是“引用”该解决方案的正确方式,但现在,每当我运行页面时,都会引发以下错误Uncaught ReferenceError:上下文未定义

有人有什么想法吗?并添加,我的 html 如下

<body>
<script id="employee-tpl" type="text/x-handlebars-template">
  <div class='details'>
    <img class='employee-image' src='img/{{firstName}}_{{lastName}}.jpg' />
    <ul>
      <li><a href="#map/{{this.id}}">See on map</a></li>         
      <li>Share with friends</li>
      <li><div id="map-canvas"/></li>
    </ul>
  <script src="lib/iscroll.js"></script>
  <script src="lib/handlebars.js"></script>
  <script src="lib/jquery-1.8.2.min.js"></script>
  <script src="js/storage/cafe-memory-store.js"></script>
  <script type="text/javascript" src="http://maps.google.com/maps/api/js?APIKEY&sensor=true"></script> 
  <script src="js/MapView.js"></script>
  <script src="js/EmployeeView.js"></script>
  <script src="js/HomeView.js"></script>
  <script src="js/main.js"></script>
</body>

最佳答案

在这一行中:

$("employee-tpl").append(HTMLtemplate(context))

您正在传递一个未定义函数和一个未定义变量(上下文)。 context 应包含您在要替换的 HandlebarJS 模板中定义的 id 参数

<a href="#map/{{this.id}}">See on map</a>
在您的情况下,

HTMLTemplateEmployeeView.template ,它保存 HandlebarJS 编译模板:它是一个接收 context 变量作为参数的函数(或者可能是employee变量?)

如果我没猜错的话,你的代码应该是:

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 传递给模板,因为我在模板中看到了 first_namelast_name

关于javascript - 上下文未定义 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20373278/

相关文章:

jquery - 打开并显示我的 jquery 对话框时出现问题

javascript - 编写一个时间函数,在游戏过程中清除倒计时并重新开始

javascript - 如何让固定滚动的 div 与窗口的其余部分一起滚动?

javascript - 自动聚焦表单中事件 div 内的第一个表单元素

jquery - 使用 jQuery 重新排序 div 位置?

javascript - 获取原始背景图像 - JQuery

javascript - 仅在禁用 javascript 时执行操作

javascript - 获取多维数组的最大值

javascript - setState更新完成后可以执行函数吗?

javascript - Express.js 接下来类似于使用生成器的功能