我的Dojo应用程序包含几个小部件,它们都是在dijit.registry (dijit.WidgetSet的一个实例)中自动注册的。我想使用filter() (Link)或map() (Link)方法将全局更改应用于某些小部件,这些小部件由方法的回调函数中定义的自定义属性模式过滤。
console.log(dijit.registry);转储注册表证明它充满了小部件:

为了测试filter()方法,我执行了以下操作(与上面提到的console.log(dijit.registry);的作用域完全相同):
var widgets = dijit.registry.filter(function(w, i) {
return true;
});
console.log(widgets);但我得到了以下输出:

使用map()方法时的类似行为:
var widgets = dijit.registry.map(function(w) {
return w;
});
console.log(widgets);..。然后我得到一个空数组。
这是怎么回事,出了什么问题?
仅供参考:在回调函数中创建单个console.log(w);不会输出任何内容,它们甚至不会被调用,这意味着dijit.registry甚至不会被这两个方法迭代。
console.log(dijit.registry._hash);打印包含12个属性(小部件)的对象。for(var w in dijit.registry._hash) { /* ... */ }根本不起作用--它不会跳入循环。
发布于 2011-09-19 01:06:03
代码片段
var widgets = dijit.registry.filter(function(w, i) {
return true;
});
console.log(widgets);没有在dojo.addOnLoad(function() { /* ... */ });中调用,这导致创建的窗口小部件还没有注册,并且不能通过dijit.registry.filter()迭代。
发布于 2011-09-19 20:45:10
您的小部件是如何创建的?以编程方式?声明性的?dijit.byId('someId')是否正确地返回您的小部件?WidgetSet#filter函数实际上并不做任何特殊的事情(1.6.1源代码):
filter: function(/*Function*/ filter, /* Object? */thisObj){
// summary:
// Filter down this WidgetSet to a smaller new WidgetSet
// Works the same as `dojo.filter` and `dojo.NodeList.filter`
//
// filter:
// Callback function to test truthiness. Is passed the widget
// reference and the pseudo-index in the object.
//
// thisObj: Object?
// Option scope to use for the filter function.
//
// example:
// Arbitrary: select the odd widgets in this list
// | dijit.registry.filter(function(w, i){
// | return i % 2 == 0;
// | }).forEach(function(w){ /* odd ones */ });
thisObj = thisObj || dojo.global;
var res = new dijit.WidgetSet(), i = 0, id;
for(id in this._hash){
var w = this._hash[id];
if(filter.call(thisObj, w, i++, this._hash)){
res.add(w);
}
}
return res; // dijit.WidgetSet
},https://stackoverflow.com/questions/7455542
复制相似问题