【问题标题】:Requirejs + Backbone Uncaught TypeError: Cannot read property 'each' of undefinedRequirejs + Backbone Uncaught TypeError:无法读取未定义的属性“每个”
【发布时间】:2014-12-27 00:56:38
【问题描述】:

错误指向backbone.js:219 库。错误是在我的 main.js 文件中的 require(['app'], function.

main.js 文件:

require.config({
  baseUrl: 'scripts',

  paths: {
    app: '../app',
    jquery: 'jquery',
    underscore: 'underscore',
    backbone: 'backbone',
    router: '../router'
  },

  shim: {
        backbone: {
            deps: ['jquery','underscore'],
            exports: 'Backbone'
        }
    }
});

require(['app'], function (App) {
    App.initialize();
});

我的 app.js 文件:

define([
  'jquery',
  'underscore',
  'backbone',
  'router' // Request router.js
], function($, _, Backbone, Router){

  var initialize = function(){
    // Pass in our Router module and call it's initialize function
    Router.initialize();
  };

  return {
    initialize: initialize
  };
});

我的 router.js 文件:

define(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {


  'use strict';

  // Router
  var AppRouter = Backbone.Router.extend({

    routes:{
      "":"list",
      "wines/:id":"wineDetails"
    },

    list:function () {
      // ...
    },

    wineDetails:function (id) {
      // ...
    }
  });
  var initialize =  function(){
    var app_router = new AppRouter;
    Backbone.history.start();
  };

  return {
    initialize: initialize
  };
});

这个问题似乎类似于this one,但没有一个建议对我有用。

【问题讨论】:

    标签: javascript backbone.js requirejs


    【解决方案1】:

    router.js 有两个错误。

    1. 使用“define”替换“require”;
    2. 返回对象必须包含“初始化”方法;

    这样的代码,

    define(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) {
      'use strict';
    
      // Router
      var AppRouter = Backbone.Router.extend({
    
        routes:{
          "":"list",
          "wines/:id":"wineDetails"
        },
    
        list:function () {
          // ...
        },
    
        wineDetails:function (id) {
          // ...
        }
      });
      var initialize =  function(){
        var app_router = new AppRouter;
        Backbone.history.start();
      }
      return {
        initialize: initialize
      };
    });
    

    【讨论】:

    • 这些都是很好的更正,但它们并没有解决我的问题。仍然得到完全相同的错误。
    • @ltrainpr,我已经像这样编辑了 router.js,没有错误。你只能保留:define(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) { 'use strict'; return { initialize: function(){ Backbone.history.start() ; } }; });
    • 嗨马林。我没有说你更新的 router.js 文件有错误。我将您的代码复制并粘贴到 router.js 中,但没有解决我的问题。我仍然在控制台中收到 TypeError。
    • 我已根据您的建议更新了 router.js 文件并更新了问题中的代码。
    • @ltrainpr ,我在dropbox上上传了一个样本backbone-require.js
    【解决方案2】:

    嗯嗯

    require.config({
      baseUrl: 'scripts',
    
      paths: {
        app: '../app',
        jquery: 'jquery',  //does this actually point to a jquery source file?
        underscore: 'underscore', //does this actually point to a underscore source file?
        backbone: 'backbone', //does this actually point to a backbone source file?
        router: '../router'
      },
    
      shim: {
            backbone: {
                deps: ['jquery','underscore'],
                exports: 'Backbone'
            }
        }
    });
    
    require(['app'], function (App) {
        App.initialize();
    });
    

    我的第一个猜测是,在某些时候 RequireJS 需要找出 jQuery、Backbone、Underscore 等的 src/可执行文件在哪里。

    【讨论】:

      猜你喜欢
      • 2013-01-15
      • 2021-04-28
      • 2017-07-20
      • 1970-01-01
      • 2020-03-28
      • 2023-04-11
      • 2016-04-22
      • 2015-06-08
      • 2014-03-17
      相关资源
      最近更新 更多