2

有没有人在 r.js 构建中成功加载了 moment.js(使用杏仁)?

我正在使用 backgrid 和 backgridMomentCell:在我构建我的 main.min.js 文件之前一切正常。在构建时间时刻未定义后,因此无法通过 backgridMomentCell 扩展找到。

我尝试了几个选项(甚至垫片)但没有成功。

如果 somedoby 有一个 require.config 可以工作,他/她可以分享吗?

编辑(很抱歉没有尽快回答,发布时间让我远离 SO):

在构建文件 BackGridMomentCell 中不断抛出“未定义时刻”错误。

我在评论中要求的代码


requirejs.config({
    paths: {
        backbone: 'vendor/backbone-1.1.0',
        backbonePageable: 'vendor/backbone-pageable-1.4.1',
        backgrid: 'vendor/backgrid/js/backgrid-0.2.6',
        backgridPaginator: 'vendor/backgrid/js/extensions/paginator/backgrid-paginator',
        backgridMomentCell: 'vendor/backgrid/js/extensions/moment-cell/backgrid-moment-cell',
        bootstrap: 'vendor/bootstrap/js/bootstrap-3.0.1',
        bootstrapDatepicker: 'vendor/bootstrap-datepicker/bootstrap-datepicker-fda46bb',
        codemirror: 'vendor/codemirror/js/codemirror-3.20',
        codemirrorMarkdown: 'vendor/codemirror/mode/markdown/markdown',
        jsDiff: 'vendor/diff-1.0.7',
        fullCalendar: 'vendor/fullcalendar/fullcalendar-1.6.4',
        fullCalendarJqueryUiCustom: 'vendor/fullcalendar/jquery-ui-1.10.3.custom.min',
        jquery: 'vendor/jquery-1.10.2',
        marked: 'vendor/marked-0.2.10',
        select2: 'vendor/select2/select2-3.4.5',
        speakingurl: 'vendor/speakingurl-0.4.0',
        underscore: 'vendor/underscore-1.5.2',
        moment: 'vendor/moment.with.langs'

    },
    shim: {
        backbone: {
            deps: ['jquery', 'underscore'],
            exports: 'Backbone'
        },
        backgrid: {
            deps: ['jquery', 'backbone', 'underscore'],
            exports: 'Backgrid'
        },
        backgridPaginator: {
            deps: ['backgrid']
        },
        backgridMomentCell: {
            deps: ['backgrid','moment']
        },
        bootstrap: {
            deps: ['jquery']
        },
        bootstrapDatepicker: {
            deps: ['jquery']
        },
        codemirror: {
            exports: 'CodeMirror'
        },
        codemirrorMarkdown: {
            deps: ['codemirror'],
            exports: 'codemirrorMarkdown'
        },
        fullCalendar: {
            deps: ['jquery', 'fullCalendarJqueryUiCustom']
        },
        fullCalendarJqueryUiCustom: {
            deps: ['jquery']
        },
        select2: {
            deps: ['jquery']
        },
        underscore: {
            exports: '_'
        }
    }
});

我的模块的负责人


define([
    'jquery',
    'fullCalendar',
    'underscore',
    'backgrid',
    'backgridPaginator',
    'moment',
    'backgridMomentCell',
    'backbone',
    'collections/ItemPaginatedCollection',
    'utils/BackgridCustomUriCell'
], function ($, _fullCalendar, _, Backgrid, _backgridPaginator, moment, MomentCell,Backbone, ItemPaginatedCollection, BackgridCustomUriCell) {

"use strict";

....

编辑 3:

在我编译的 main.js 工作之前加载 moment.js 但不是最佳恕我直言。

4

3 回答 3

1

我发现的最简单的两种方法:

1)在 RequireJS API 中指定的CommonJS 包装器代码中包装 backgrid-moment-cell.js 。您可以使用 API 中提到的转换工具在构建过程中动态执行此操作。

2) 需要内联矩单元并将 findNestedDependencies 在 build.js 中设置为 false。

问题是矩单元代码在this初始化时会传递给自身,并查找“exports.moment”或“this.moment”。

在相关的说明中,在 shim 中为 moment-cell 包括 moment 是不必要的,从技术上讲,您应该包括下划线。 从 API

仅使用其他“填充”模块作为填充脚本的依赖项,或没有依赖项的 AMD 库,并在它们也创建全局(如 jQuery 或 lodash)之后调用 define()。否则,如果您使用 AMD 模块作为 shim 配置模块的依赖项,则在构建之后,该 AMD 模块可能直到构建中的 shimed 代码执行后才被评估,并且将发生错误。最终的解决方法是升级所有填充代码以具有可选的 AMD define() 调用。

Moment 实际上是一个 AMD 模块。

于 2014-03-20T16:11:02.953 回答
0

我认为这与不推荐使用时刻的全局对象有关。

2.4.0 - Deprecate globally exported moment, will be removed in next major

https://github.com/moment/moment

现在需要在特定模块中定义时刻。

于 2014-01-15T21:08:58.767 回答
0

您的问题很可能是您没有findNestedDependencies在构建文件中指定 true 。

看看这个提交(来自我关于 RquireJS 和 Backbone.Marionette 的书):https ://github.com/davidsulc/structuring-backbone-with-requirejs-and-marionette/commit/85d3d3dd40d0cebd858224c3903a12d6998d668d

于 2014-01-13T08:44:42.727 回答