4

Backbone 应用程序经过预优化后运行良好。使用 r.js(与杏仁)后,我成功生成了一个输出文件,该文件似乎总是在紧跟骨干的模块中抛出“TypeError:Backbone is undefined”错误。

例如,当 r.js 的输出如下所示时:

/consumer/public/javascripts/vendor/almond.js
/consumer/public/javascripts/vendor/jquery-1.7.2.js
/consumer/public/javascripts/vendor/underscore.js
/consumer/public/javascripts/vendor/bootstrap.js
/consumer/public/javascripts/vendor/backbone.js
/consumer/public/javascripts/rjs/src/mnp/prop_m.js
/consumer/public/javascripts/rjs/src/mnp/prop_c.js
/consumer/public/javascripts/vendor/play-mustache.js
...
/consumer/public/javascripts/main.js

在浏览器中加载优化文件后,在引用 Backbone 时会在 'prop_m.js' 模块中抛出上述错误。使用 require 2.0.5、Backbone 0.9.2 和 almond 0.1.4。提前感谢您的帮助。

PS:build.js 看起来像这样:

    ({
    baseUrl:   "./",
    mainConfigFile: "main.js",
    paths: {
    'almond': 'vendor/almond',

        // need a path to the cs-compiled .js file
        'app' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/app',
        'mnp/router' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/router',
        'mnp/search_form_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/search_form_v',
        'mnp/prop_c' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_c',
        'mnp/prop_m' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_m',
        'mnp/prop_list_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_list_v',
        'mnp/prop_item_v' : '../../target/scala-2.9.1/resource_managed/main/public/javascripts/rjs/src/mnp/prop_item_v'
    },


    /*not compatible with modules*/
    name: 'vendor/almond',
    include: ["main"],

    out: "dist/app.js",
    /*not compatible with modules*/

    keepBuildDir: false,

    preserveLicenseComments: false,
    optimize: "none", 
    wrap: {
        start: "(function(global, define) {\n"+
           // check for amd loader on global namespace
            "  var globalDefine = global.define;\n",

        end:   "  var library = require('main');\n"+
            "  if(typeof module !== 'undefined' && module.exports) {\n"+
            // export library for node
            "    module.exports = library;\n"+
            "  } else if(globalDefine) {\n"+
            // define library for global amd loader that is already present
            "    (function (define) {\n"+
            "      define(function () { return library; });\n"+
            "    }(globalDefine));\n"+
            "  } else {\n"+
            // define library on global namespace for inline script loading
            "    global['main'] = library;\n"+
            "  }\n"+
            "}(this));\n"
    }


})`
4

3 回答 3

2

FWIW:如果有人遇到这个问题……使用 amd 版本的主干和下划线似乎可以解决问题。IE。amd 版本有效,垫片没有

使用 RequireJS 加载 Backbone 和 Underscore

于 2012-09-24T19:28:04.597 回答
1

当我忘记包含我shim的 s 时,我遇到了这个问题。我通过在构建配置中设置mainConfigFile我的main.js文件来解决它,例如如下所示:https ://github.com/jrburke/r.js/blob/master/build/example.build.js

于 2012-09-28T22:08:15.333 回答
0

您可以尝试使用inlineText: true以确保主干在您的编译.js文件中

此外,为了避免关注存储路径的 2 个对象,我会推荐类似的东西mainConfigFile: "main.js"- 像魅力一样工作 ;) 编辑:你为什么要设置一个mainConfigFile选项路径?

于 2012-09-23T07:15:11.267 回答