简体   繁体   English

无法在Backbone&RequireJS应用中解析_

[英]Unable to resolve _ in a Backbone & RequireJS app

I'm relatively new to Backbone and RequireJS, so please bear with me. 我对Backbone和RequireJS相对较新,所以请耐心等待。 I'm getting an error when I do the following in my collection: _.range(0,10) . 在集合中执行以下操作时出现错误: _.range(0,10) . _.range(0,10) It's giving me this error: 这给了我这个错误:

Uncaught TypeError: Cannot call method 'range' of undefined 未捕获的TypeError:无法调用未定义的方法“范围”

Somehow the "_" is not getting resolved when my Collection is loaded. 加载我的收藏集后,“ _”无法以某种方式解析。 Here's my collection below: 以下是我的收藏:

define([
  'jquery',
  'underscore',
  'backbone',
  'collections/feed',
  'text!/static/templates/shared/display_item.html'
], function($, _, Backbone, FeedCollection, DisplayItem){
  debugger;   // Added this to test the value of _
  var FeedView = Backbone.View.extend({
    el: '#below-nav',
    initialize: function () {
      this.feedCollection = new FeedCollection();
    },
    feed_row: '<div class="feed-row row">',
    feed_span8: '<div class="feed-column-wide span8">',
    feed_span4: '<div class="feed-column span4">',
    render: function () {
      this.loadResults();
    },
    loadResults: function () {
      var that = this;
      // we are starting a new load of results so set isLoading to true
      this.isLoading = true;


      this.feedCollection.fetch({
        success: function (articles) {
          var display_items = [];

          // This line below is the problem...._ is undefined
          var index_list = _.range(0, articles.length, 3);

          _.each(articles, function(article, index, list) {
            if(_.contain(index_list, index)) {
              var $feed_row = $(that.feed_row),
                    $feed_span8 = $(that.feed_span8),
                    $feed_span4 = $(that.feed_span4);

              $feed_span8.append(_.template(DisplayItem, {article: articles[index]}));
              $feed_span4.append(_.template(DisplayItem, {article: articles[index+1]}));
              $feed_span4.append(_.template(DisplayItem, {article: articles[index+2]}));
              $feed_row.append($feed_span8, $feed_span4);
              $(that.el).append($feed_row);
            }
          });
        }
      });
    }
  });
  return FeedView;
});

I added the debugger line so that I could test the values of all the arguments. 我添加了调试器行,以便可以测试所有参数的值。 Everything loaded fine, except for _. 一切正常,除了_。 Could this be something wrong with my config.js file? 我的config.js文件可能出问题了吗?

require.config({

  // Set base url for paths to reference
  baseUrl: 'static/js',

  // Initialize the application with the main application file.
  deps: ['main'],
  paths: {
    jquery: 'libs/jquery/jquery.min',
    require: 'libs/require/require.min',
    bootstrap: 'libs/bootstrap/bootstrap.min',
    text: 'libs/plugins/text',
    underscore: 'libs/underscore/underscore',
    backbone: 'libs/backbone/backbone',
    json: 'libs/json/json2',
    base: 'libs/base/base'
  },
  shim: {
    'backbone': {
      // These script dependencies should be loaded first before loading
      // backbone
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    },
    'bootstrap': {
      deps: ['jquery'],
      exports: 'Bootstrap'
    }
  }
})

Your help is greatly appreciated. 非常感谢您的帮助。 My head is spinning as a result of this error. 由于这个错误,我的头在旋转。

Based off the project that I'm working on, you need a shim for underscore as well. 基于我正在从事的项目,您还需要一个下划线垫片。 Underscore isn't 'exported' per say, so use this instead: 下划线不是说“导出”的,因此请改用以下命令:

shim: {
    'backbone': {
      // These script dependencies should be loaded first before loading
      // backbone
      deps: ['underscore', 'jquery'],
      exports: 'Backbone'
    },
    'bootstrap': {
      deps: ['jquery'],
      exports: 'Bootstrap'
    },
    'underscore': {
      exports: '_'
    }
  }

Seems this might also be 'duplicate' question of Loading Backbone and Underscore using RequireJS - one or two of the answers down the list there is a mention of this setup. 似乎这也可能是使用RequireJS加载Backbone和Underscore的 “重复”问题-列表中的一两个答案中提到了此设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM