0

我正在一个带有骨干样板的项目中工作,该项目使用 RequireJS 加载模块和 JamJS 来管理它们。我的要求config.js文件如下:

require.config({
  deps: ["../vendor/jam/require.config", "main"],
  paths: {
    "backbone.localStorage": "../vendor/backbone.localStorage-1.0/backbone.localStorage"
  },
  shim: {
    "backbone.localStorage": {
        deps: ['backbone']
    }
  }
});

如您所见,来自 jam 配置文件的 RequireJS 加载配置jam/require.config.js指定了主干、jquery 和下划线。接下来是一段文件:

    "packages": [
        {
            "name": "backbone.layoutmanager",
            "location": "../vendor/jam/backbone.layoutmanager",
            "main": "backbone.layoutmanager.js"
        }
        {
            "name": "backbone",
            "location": "../vendor/jam/backbone",
            "main": "backbone.js"
        }
    ],
    "version": "0.2.11",
    "shim": {
        "backbone.layoutmanager": {
            "deps": [
                "jquery",
                "backbone",
                "lodash"
            ],
            "exports": "Backbone.LayoutManager"
        }
        "backbone": {
            "deps": [
                "jquery",
                "lodash"
            ],
            "exports": "Backbone"
        }
    }
};

我想要在我config.js的主干.localStorare 库中指定取决于包部分中定义的主干。此外,如果我按如下方式指定主干路径,那么它会找到主干库,但会发现一条错误消息,指出未加载下划线:

require.config({
  deps: ["../vendor/jam/require.config", "main"],
  paths: {
    "backbone": "../vendor/jam/backbone/backbone",
    "backbone.localStorage": "../vendor/backbone.localStorage-1.0/backbone.localStorage"
  },
  shim: {
    "backbone.localStorage": {
        deps: ['backbone']
    }
  }
});

我可以使用 jamjs 添加backbone.localStorage 库,但是可用包的版本很旧,所以我更喜欢手动下载最后一个并手动包含。

任何想法或帮助将不胜感激。

4

2 回答 2

2

感谢asgoth,我可以解决问题。我把文件放在下面。

使用此配置,我可以将 shim 库放置在我的config.js文件中,这取决于文件中由 jam 管理的包require.config.js

config.js

// Set the require.js configuration for your application.
require.config({

  // Initialize the application with the main application file and the JamJS
  // generated configuration file.
  deps: ["../vendor/jam/require.config", "main"],

  // Packeges defined at jam/require.config.js required by shim libraries
  packages: [
    {"name" : "backbone"}, 
    {"name" : "jquery"}
  ],

  paths: {
    // Put paths here.
    "codemirror" : "../vendor/codemirror-3.0/lib/codemirror",
    "codemirror-markdown": "../vendor/codemirror-3.0/mode/markdown/markdown",
    "backbone.localStorage": "../vendor/backbone.localStorage-1.0/backbone.localStorage",
    "bootstrap" : "../vendor/bootstrap-2.2.2/js/bootstrap"
  },

  shim: {
    // Put shims here.
    "codemirror-markdown": {
      deps: ["codemirror"]
    },
    "backbone.localStorage": {
      deps: ['backbone']
    },
    "bootstrap": {
      deps: ['jquery']
    }
  }

});

require.config.js

var jam = {
    "packages": [
        {
            "name": "jquery",
            "location": "../vendor/jam/jquery",
            "main": "jquery.js"
        },
        {
            "name": "backbone.layoutmanager",
            "location": "../vendor/jam/backbone.layoutmanager",
            "main": "backbone.layoutmanager.js"
        },
        {
            "name": "underscore",
            "location": "../vendor/jam/underscore",
            "main": "underscore.js"
        },
        {
            "name": "backbone",
            "location": "../vendor/jam/backbone",
            "main": "backbone.js"
        },
        {
            "name": "lodash",
            "location": "../vendor/jam/lodash",
            "main": "./lodash.js"
        }
    ],
    "version": "0.2.11",
    "shim": {
        "backbone.layoutmanager": {
            "deps": [
                "jquery",
                "backbone",
                "lodash"
            ],
            "exports": "Backbone.LayoutManager"
        },
        "underscore": {
            "exports": "_"
        },
        "backbone": {
            "deps": [
                "jquery",
                "lodash"
            ],
            "exports": "Backbone"
        }
    }
};

if (typeof require !== "undefined" && require.config) {
    require.config({packages: jam.packages, shim: jam.shim});
}
else {
    var require = {packages: jam.packages, shim: jam.shim};
}

if (typeof exports !== "undefined" && typeof module !== "undefined") {
    module.exports = jam;
}
于 2013-01-03T20:31:33.763 回答
1

Backbone 需要Underscore 和 jQuery

shim: {
    "backbone": {
       "deps": [ "underscore", "jquery" ],
       "exports": "Backbone"  //attaches "Backbone" to the window object
    },
    "underscore": {
       "exports": "_"
    }
}

所以我认为你最好调整你的jam/require.config.js.

编辑:

接下来,packages您问题中的部分,这是jam/require.config.js? 位置是否相对于 的位置jam/require.config.js

您是否尝试packages在自己的 config.js 中添加一个部分:

"packages": ["backbone", "backbone.layoutmanager"]
于 2012-12-31T20:20:39.480 回答