首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >context is not defined JavaScript

context is not defined JavaScript
EN

Stack Overflow用户
提问于 2013-12-04 18:48:03
回答 1查看 11.5K关注 0票数 2

参考我在这里遇到的原始问题,在做了大量的谷歌搜索之后,我找到了一个可行的解决方案,那就是这个How to append multiple Google Maps areas to divs using Handlebars

然而,我用下面的方式实现了我的

代码语言:javascript
复制
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: context is not defined

有人有什么想法吗?另外,我的html如下所示

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2013-12-04 19:00:07

在这一行中:

代码语言:javascript
复制
$("employee-tpl").append(HTMLtemplate(context))

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

代码语言:javascript
复制
<a href="#map/{{this.id}}">See on map</a>

在本例中,HTMLTemplateEmployeeView.template,它包含HandlebarJS编译后的模板:它是一个接收context变量(或者employee变量?)作为参数的函数。

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

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20373278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档