首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >dijit.registry.filter / dijit.registry.map将不会迭代,尽管dijit.registry中充满了注册的小部件?

dijit.registry.filter / dijit.registry.map将不会迭代,尽管dijit.registry中充满了注册的小部件?
EN

Stack Overflow用户
提问于 2011-09-17 22:40:28
回答 2查看 489关注 0票数 1

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

console.log(dijit.registry);转储注册表证明它充满了小部件:

为了测试filter()方法,我执行了以下操作(与上面提到的console.log(dijit.registry);的作用域完全相同):

代码语言:javascript
复制
var widgets = dijit.registry.filter(function(w, i) {
    return true;
});

console.log(widgets);

但我得到了以下输出:

使用map()方法时的类似行为:

代码语言:javascript
复制
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) { /* ... */ }根本不起作用--它不会跳入循环。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-19 01:06:03

代码片段

代码语言:javascript
复制
var widgets = dijit.registry.filter(function(w, i) {
    return true;
});

console.log(widgets);

没有在dojo.addOnLoad(function() { /* ... */ });中调用,这导致创建的窗口小部件还没有注册,并且不能通过dijit.registry.filter()迭代。

票数 1
EN

Stack Overflow用户

发布于 2011-09-19 20:45:10

您的小部件是如何创建的?以编程方式?声明性的?dijit.byId('someId')是否正确地返回您的小部件?WidgetSet#filter函数实际上并不做任何特殊的事情(1.6.1源代码):

代码语言:javascript
复制
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
},
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7455542

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档