簡體   English   中英

無法在Backbone&RequireJS應用中解析_

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

我對Backbone和RequireJS相對較新,所以請耐心等待。 在集合中執行以下操作時出現錯誤: _.range(0,10) . _.range(0,10) 這給了我這個錯誤:

未捕獲的TypeError:無法調用未定義的方法“范圍”

加載我的收藏集后,“ _”無法以某種方式解析。 以下是我的收藏:

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

非常感謝您的幫助。 由於這個錯誤,我的頭在旋轉。

基於我正在從事的項目,您還需要一個下划線墊片。 下划線不是說“導出”的,因此請改用以下命令:

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加載Backbone和Underscore的 “重復”問題-列表中的一兩個答案中提到了此設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM