要求JS如何访问所需的对象
requirejs how to access required object
我有:
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() : {};
});
module.config()
需要在"需要配置"中具有相应的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
}
}
});
参考这里。
通过上述设置,代码:
require(['config'], function(config) {
console.log(config.xxx);
});
。将输出5
.但是您将其用作config.filters.paymentterms
.这可以通过简单的配置进行配置吗?
相关文章:
- 如何使用此从对象访问构造函数
- LokiJS与标准Javascript对象访问
- JavaScript子对象访问自己的值
- Javascript嵌套对象访问根级别
- 即使使用 angular.fromJson() 也无法从 json 对象访问字符串
- 从表单到 Ajax 的动态对象访问
- 将 NodeJS 模块范围变量作为对象访问
- 能够从Meteor中的辅助对象访问特定值
- 从JavaScript对象访问数据's数组成员变量
- 如何从 React 中的事件对象访问自定义属性
- 函数中的对象访问;不起作用
- 如何在javascript中从JSON对象访问数组
- 如何从C#.Net Webbrowser对象访问javascript变量
- Javascript中最快的类/对象访问/实例化是什么
- 覆盖对象访问 JavaScript
- 如何从 jquery 中的另一个对象访问在一个对象中声明的变量
- 从 JavaScript 对象访问变量
- 如何从范围对象访问数据到指令
- 从vue.js中的对象访问v-repeat-overarray中的属性
- jQuery:从具有一个类的多个实例的对象访问元素