-1

我对 Backbone 和 RequireJS 比较陌生,所以请多多包涵。在我的收藏中执行以下操作时出现错误: _.range(0,10). 它给了我这个错误:

未捕获的类型错误:无法调用未定义的方法“范围”

加载我的收藏时,不知何故“_”没有得到解决。下面是我的收藏:

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

我添加了调试器行,以便我可以测试所有参数的值。一切都很好,除了_。这可能是我的 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'
    }
  }
})

非常感谢您的帮助。由于这个错误,我的头在旋转。

4

1 回答 1

2

根据我正在从事的项目,您还需要一个用于下划线的垫片。下划线不是“导出”的,所以请改用:

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

似乎这也可能是使用 RequireJS 加载主干和下划线的“重复”问题——列表中的一两个答案提到了这个设置。

于 2012-12-04T20:20:59.213 回答