Requirejs 错误的依赖顺序

Requirejs wrong dependencies order

我在 js 文件中保存了一个变量:Subcategories.js。像这样的东西(但实际上要大得多):

define({
    subcategories: { 
        "Category1":
        [
            "Subcategory1-1", "Subcategory1-2", "Subcategory1-3"
        ],
        "Electrical":
        [
            "Subcategory2-1", "Subcategory2-2", "Subcategory2-3", "Subcategory2-4"
        ],
    },
    getSubcategories: function () {
        return this.subcategories;
    }
});

我已经成功地通过这种方式将变量发送到 SomethingController.js(只复制了下面必要代码的一部分):

define(
 [
           "underscore",
           "base/Controller",
           "views/SomethingView",
           "collections/SomethingCollection",
           "helpers/Subcategories",
 ], 
 function(_, Controller, SomethingView, SomethingCollection, Subcategories) {
  var subcategory = null,
  
  var SomethingController = Controller.extend({
   show_results: function(collection, resp) {
    var data = { 
     subcategories: Subcategories.getSubcategories(),
    };
        ...

但是当我尝试在 SomethingView.js 中导入变量时,它只有在我再次使用整个 json 对象时才有效:

define(
 [
  "jquery",
  "backbone",
  "has/form",
  "has!input-attr-placeholder?:jqueryui/placeholder",
  "scripts/libs/jquery.tmpl.js",
  "helpers/Subcategories",
 ], 
 function( $, Backbone, viewTemplate, has, Subcategories) {
  
  var SomethingItemsView = Backbone.View.extend({
   el: $("#main-section"),
   template: viewTemplate,
   events: {
    "click #some_button" : "change_some_page",
   },
   something_items: function( event ) {
    event.preventDefault();
    var somethingCategory = $("#some_selector");
    var subcategories = {// entire json object
        }
        var somethingSubcategory =  subcategories[somethingCategory];
        ...

我已经尝试了 var subcategories = Subcategories.getSubcategories()Subcategories.subcategories 和各种方法。为这么小的事情浪费了相当多的时间。我很困惑为什么它不起作用,我不想将整个对象硬编码在那里。

我正在对现有网站的前端进行一些更改,因此请注意,我不能更改任何这些文件的框架,除了 Subcategories.js.

如有任何意见,我们将不胜感激。

我认为你的问题出在依赖项的顺序上:

[
        "jquery",
        "backbone",
        "has/form",
        "has!input-attr-placeholder?:jqueryui/placeholder",
        "scripts/libs/jquery.tmpl.js",
        "helpers/Subcategories",
    ], 
    function( $, Backbone, viewTemplate, has, Subcategories) {

它必须在同一个 order.Subcategories 是第六个也是最后一个参数,但你只有五个输入。

jquery => $
backbone => Backbone
has/form => viewTemplate
has!... => has
scripts/libs/jquery.tmpl.js => Subcategories
helpers/Subcategories => OUPS NOTHING O_O

希望对您有所帮助