【问题标题】:Backbone + Require + Datamaps : "Datamap is not defined"Backbone + Require + Datamaps :“未定义数据图”
【发布时间】:2014-06-08 21:06:16
【问题描述】:

我是 Backbone.js 的新手,我将它用于数据可视化,作为一个学校项目。 经过几周的工作,我已经按照本教程实现了 require.js:http://backbonetutorials.com/organizing-backbone-using-modules/

那时我已经重新组织了我的代码,但现在我遇到了一个我无法修复的错误...我使用 Datamaps (http://datamaps.github.io/) 创建世界地图。我在 require define() 函数中传递了所需的脚本,但我可能做错了什么。

这是提供错误的代码部分:

define([
  'jquery',
  'underscore',
  'backbone',
  'd3',
  'c3',
  'topojson',
  'datamaps',
  'jqueryui',
  'text!templates/map.html'
], function($, _, Backbone, mapTemplate){

     var MapView = Backbone.View.extend({

       el: $('.container'),

       initialize: function(){
         var _this = this;

         var map = new Datamap({ ... })
...

浏览器以“Uncaught ReferenceError: Datamap is not defined”响应。 它以前工作过,由于我使用了 require,它不再工作了,我可能错过了一个参数或其他东西。

我将不胜感激;)

提前谢谢你!

【问题讨论】:

  • 您的依赖项与您发布的代码中的函数参数不匹配(在d3 -> mapTemplate 之后)
  • 哦,好吧,你明白了:)谢谢!我在 require 上阅读了 define() 文档,实际上我不明白我真正在做什么,我认为这只是为了加载脚本。

标签: javascript backbone.js requirejs


【解决方案1】:

使用 RequireJS 时,当您定义具有依赖项的模块时,依赖项将作为参数传递给您的模块定义函数。因此,您需要匹配它们。例如,

define([
  'jquery',
  'underscore',
  'backbone',
  'd3',
  'c3',
  'topojson',
  'datamaps',
  'jqueryui',
  'text!templates/map.html'
],
function($, _, Backbone, D3, C3, Topojson, Datamaps, jQueryUI) {
   // and in here you can then use the modules ...
});

【讨论】:

  • 好的,谢谢!我认为我需要定义我需要加载的每个脚本,但是即使我没有在这里定义它,例如 D3 也会被加载。现在我更好地理解了 define() 的作用;)
  • 没有问题。为了将来参考,您无需添加评论来表示感谢。只需接受答案/赞成就足够了。欢迎来到 SO。
猜你喜欢
  • 2021-09-15
  • 2013-03-19
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多