requirejs如何访问所需的对象

时间:2015-02-03 15:46:54

标签: javascript requirejs

我有:

define(['jquery', 'kendo', 'util/addMenu', 'config'], function ($, kendo, addMenu, config) {
var obj = {
    paymentTermsFilterUI: function (e) {
            e.kendoDropDownList({
                dataSource: config.filters.paymentterms,
                optionLabel: "Select payment term",
                dataTextField: "text",
                dataValueField: "id",
                change: function (e) {
                    obj.setInternalFilter('termId', 'eq', this.value())
                }
            });
        },
}

但它告诉我配置未定义。

如何访问配置?

配置:

require.config({
    baseUrl: '/laravel/public/js/app',
    paths: {
        jquery: '/laravel/public/js/jquery/jquery',
        bootstrap: '../bootstrap/bootstrap.min',
        kendo: '../kendo/kendo.all.min',
        kendo_culture: '../kendo/kendo-culture-en-GB',
        typeahead: '../typeahead',
        text: '../text',
        handlebars: '../handlebars/handlebars',
        io: 'http://view.euautomation.com:8008/socket.io/socket.io'
    },
    shim: {
        "bootstrap": {
            deps: ["jquery"]
        },
        "kendo": {
            deps: ["jquery"]
        },
        "kendo_culture": {
            deps: ["kendo"],
            exports: 'kendo'
        },
        "typeahead": {
            deps: ["jquery"]
        }
    },
    filters: {
        conditions: [
            {"id": 1, "condition": "New"},
            {"id": 4, "condition": "New - FS"},
            {"id": 6, "condition": "New in box"},
            {"id": 5, "condition": "Refurbished"}
        ],
        leadtimes: [
            {"id": 1, "name": "Not set"},
            {"id": 2, "name": "Same day"},
            {"id": 3, "name": "Next day"},
            {"id": 4, "name": "2-3 days"},
            {"id": 5, "name": "3-5 days"},
            {"id": 6, "name": "5-7 days"},
            {"id": 7, "name": "7-10 days"},
            {"id": 8, "name": "3-4 weeks"},
            {"id": 9, "name": "5-6 weeks"},
            {"id": 10, "name": "9 weeks"},
            {"id": 11, "name": "10-14 days"},
            {"id": 12, "name": "7-8 weeks"},
            {"id": 13, "name": "2-3 weeks"},
            {"id": 14, "name": "48 hours"},
            {"id": 15, "name": "10-12 weeks"}
        ],
        warranties: [
            {"id": 1, "warranty": "None"},
            {"id": 2, "warranty": "1 Month"},
            {"id": 3, "warranty": "3 Months"},
            {"id": 4, "warranty": "6 Months"},
            {"id": 5, "warranty": "12 Months"},
            {"id": 6, "warranty": "Lifetime"},
            {"id": 7, "warranty": "24 Months"},
            {"id": 8, "warranty": "DOA"},
            {"id": 9, "warranty": "14 days"}
        ],
        paymentterms: [
            {"id": 1, "text": "Advance"},
            {
                "id": 2,
                "text": "Account 14"
            },
            {
                "id": 3,
                "text": "Account 30"
            },
            {
                "id": 4,
                "text": "Account 60"
            },
            {
                "id": 5,
                "text": "Not set"
            },
            {
                "id": 6,
                "text": "Account 7"
            },
            {
                "id": 7,
                "text": "Account 45"
            },
            {
                "id": 8,
                "text": "Account 10"
            }
        ]
    }
});

define('config', function (require) {
    'use strict';

    var module = require('module');

    return module.config ? module.config() : {};
});

1 个答案:

答案 0 :(得分:0)

module.config()在require配置中需要相应的config部分。由于没有,module.config()返回undefined,这将成为config AMD模块的值。

简单地说,代码应该是:

require.config({
    // other staff as before
    config: { // per module configuration root
        'config': { // this is the configuration of the `config` module
            // configuration staff; may even be empty
            xxx: 5 // example
        }
    }
});

参考here

通过上面的设置,代码:

require(['config'], function(config) {
    console.log(config.xxx);
});

...会输出5。但是你将它用作config.filters.paymentterms。这可以通过简单的配置进行配置吗?