快速修复:
与其将其创建为全局变量,不如将其添加到广泛用于这些情况的命名空间。因此,在创建商店时,将其添加到特定的命名空间并在props
.
var ns = {};
//use this namespace for all objects subject to grid/a particular section/anything
ns.myStore = new Memory({
data: [
{name:"Option 1", id:"OP1"},
{name:"Option 2", id:"OP2"},
{name:"Option 3Samoa", id:"OP3"}
]
});
props: 'store: ns.myStore, searchAttr: "label"'
因此,我们可以避免将全局对象直接添加到窗口对象。
推荐修复:
在为该列传递的模板字符串中,使用自定义小部件,而不是使用默认组合框。
在这个小部件中,覆盖该postCreate
方法并设置所需的商店。
define([
"dojo/_base/lang",
"dijit/form/ComboBox",
"dojo/store/Memory"
], function(lang, comboBox, Memory) {
return dojo.declare("myapp.widget.MyComboBox", [ dijit.form.ComboBox], {
// summary:
//Custom sub-class of ComboBox with following functionality:
//This will set the desired store
postCreate: function(){
// summary:
// This will call default postCreate and additionally create/set store:
this.inherited(arguments);
var wid = this;
//if store is static get storeObj from a json file
//if store comes from backend, make the call here and get storeObj
dojo.xhrGet({
url: "filenameOrUrl",
handleAs: "json"
}).then(function(result){
var storeObj = new Memory(result);
wid.set("store",storeObj);
});
}
});
});
现在你在该列的模板中。请注意,我们不需要在此处将 store 作为字符串或对象提及,因为一旦创建,小部件本身就会加载 store。
<div data-dojo-type="myapp/widget/MyComboBox"
style="width: 100%"
data-dojo-attach-point="gridCellEditField"
></div>