Dojo如何扩展一个dijitclass?
Dojo how to extend a dijit class?
我现在需要在 dijit/form/Select
上添加一些自定义函数,但我收到以下错误:
Uncaught TypeError: SelectGroup is not a constructor
如何解决这个问题?
require([
'dijit/form/Select',
'dojo/_base/window',
'dojo/domReady!'
], function(Select, win) {
var SelectGroup = define([
"dojo/_base/declare",
"dijit/form/Select"
], function(declare, Select) {
return declare(Select, {
myCustom: function() {
alert('hey custom');
}
});
});
var select = new SelectGroup({
name: 'select',
options: [{
label: '<i>Colors I love</i>',
value: 'G 1',
disabled: true
}, {
label: 'Red',
value: '1'
}, {
label: 'Green',
value: '2',
selected: true
},
]
}, 'select');
select.on('change', function(value) {
alert(value);
});
});
我认为这只是你在 require 中编写 define() 时的一个错误,(AMD 嵌套调用)!
很简单,您只需进行所有导入(要求),包括声明和调用声明(使您的小部件直接),它将 return 中的新小部件 var SelectGroup
参考
请在下面找到一个工作片段:
require([
"dojo/_base/declare",
"dijit/form/Select",
'dojo/_base/window',
'dojo/domReady!'
], function(declare, Select, win) {
var SelectGroup = declare(Select, {
myCustom: function() {
alert('hey custom calue is = '+this.value);
}
});
var select = new SelectGroup({
name: 'select',
options: [{
label: '<i>Colors I love</i>',
value: 'G 1',
disabled: true
}, {
label: 'Red',
value: '1'
}, {
label: 'Green',
value: '2',
selected: true
},
]
}, 'select');
select.on('change', function(value) {
select.myCustom();
});
});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dijit/themes/claro/claro.css" />
<script>
window.dojoConfig = {
parseOnLoad: false,
async: true
};
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dojo/dojo.js"></script>
<body class="claro">
<div id="select"></div>
</body>
我现在需要在 dijit/form/Select
上添加一些自定义函数,但我收到以下错误:
Uncaught TypeError: SelectGroup is not a constructor
如何解决这个问题?
require([
'dijit/form/Select',
'dojo/_base/window',
'dojo/domReady!'
], function(Select, win) {
var SelectGroup = define([
"dojo/_base/declare",
"dijit/form/Select"
], function(declare, Select) {
return declare(Select, {
myCustom: function() {
alert('hey custom');
}
});
});
var select = new SelectGroup({
name: 'select',
options: [{
label: '<i>Colors I love</i>',
value: 'G 1',
disabled: true
}, {
label: 'Red',
value: '1'
}, {
label: 'Green',
value: '2',
selected: true
},
]
}, 'select');
select.on('change', function(value) {
alert(value);
});
});
我认为这只是你在 require 中编写 define() 时的一个错误,(AMD 嵌套调用)!
很简单,您只需进行所有导入(要求),包括声明和调用声明(使您的小部件直接),它将 return 中的新小部件 var SelectGroup
参考
请在下面找到一个工作片段:
require([
"dojo/_base/declare",
"dijit/form/Select",
'dojo/_base/window',
'dojo/domReady!'
], function(declare, Select, win) {
var SelectGroup = declare(Select, {
myCustom: function() {
alert('hey custom calue is = '+this.value);
}
});
var select = new SelectGroup({
name: 'select',
options: [{
label: '<i>Colors I love</i>',
value: 'G 1',
disabled: true
}, {
label: 'Red',
value: '1'
}, {
label: 'Green',
value: '2',
selected: true
},
]
}, 'select');
select.on('change', function(value) {
select.myCustom();
});
});
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dijit/themes/claro/claro.css" />
<script>
window.dojoConfig = {
parseOnLoad: false,
async: true
};
</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.12.1/dojo/dojo.js"></script>
<body class="claro">
<div id="select"></div>
</body>