2

我已经调试了好几天了。Store.findBy(function (record, id) 没有表现。或者也许是我行为不端。已将 JSON 放入代码中,以便于测试。FindBy() 匹配 12345,但没有 12345,它返回索引 0 和 foo。我在做什么?

Ext.application({
    name: 'MyApp',
    launch: function() {

        var theJson = {
            "users": [{
                "user": {
                    "id": 0 ,
                    "name": "foo" ,
                    "age": 22 ,
                    "skills": [{
                        "type": "bowcrafting" ,
                        "skillLevel": 50 ,
                        "levels": [10, 25, 50, 75, 90, 95, 99, 100]
                    }]
                }} , {
                "user": {
                    "id": 1 ,
                    "name": "bar" ,
                    "age": 71 ,
                    "skills": [{
                        "type": "fencing" ,
                        "skillLevel": 32 ,
                        "levels": [10, 25, 50, 90, 95, 99, 100]
                    } , {
                        "type": "swordsmanship" ,
                        "skillLevel": 73 ,
                        "levels": [10, 25, 50, 75, 80, 85, 90, 95, 99, 100]
                    }]
                }} , {
                "user": {
                    "id": 2 ,
                    "name": "foobar" ,
                    "age": 132 ,
                    "skills": [{
                        "type": "tactics" ,
                        "skillLevel": 90 ,
                        "levels": [10, 25, 50, 90, 95, 99, 100]
                    } , {
                        "type": "carpentery" ,
                        "skillLevel": 86 ,
                        "levels": [10, 25, 50, 75, 90, 95, 99, 100]
                    } , {
                        "type": "hiding" ,
                        "skillLevel": 100 ,
                        "levels": [10, 25, 50, 65, 75, 80, 85, 90, 95, 99, 100]
                    }]
                }
            }]
        };

        var jstore = Ext.create ('Ext.data.Store', {
            fields: ['id', 'name', 'age', 'skills'] ,
            data : theJson,
            proxy: {
                type: 'memory' ,
                reader: {
                    type: 'json' ,
                    root: 'users' ,
                    record: 'user' ,
                    idProperty: 'id'
                }
            } ,

            autoLoad: true
        });

        Ext.create ('Ext.button.Button', {
            text: 'Push me' ,
            renderTo: Ext.getBody () ,
            handler: function (btn) {
                var index = jstore.findBy (function (user, id) {
                    // Here's the hint
                    if (user.data.skills.skillLevel === 12345) return id;
                    else return -1;
                });

                console.log ('index = ', index);

                if (index != -1) {
                    // It will print 'foo' because it's the user
                    // that has the skillLevel equal to 50
                    console.log (jstore.getAt(index).get ('name'));
                };

                if (index === -1) {
                    // It will print 'foo' because it's the user
                    // that has the skillLevel equal to 50
                    console.log ('Failed');
                }
            }
        });
    }
}); 
4

2 回答 2

4

读过文档吗?你的findBy方法不符合合同。特别是,-1如果不匹配,则返回,并且由于 this 是 JavaScript,因此-1is true,因此找到了第一条记录。

        handler: function (btn) {
            var index = jstore.findBy (function (user, id) {
                // Here's the hint
                console.log(id);
                console.log(user);

                if (user.data.skills.skillLevel === 12345) return true;
                else return false;
            });

(这解决了您的误报问题,我并不是说您的支票会找到任何东西)。

于 2012-10-18T05:19:38.203 回答
-1

你有嵌套数组的 json 数据。

store.findBy(function(user,id) 将为存储中的每条记录调用。记录skills是一个数组。所以你需要再次迭代它。

   var skills = user.get('skills');

   Ext.each(skills,function(skill,idx){
         if(skill.skillLevel == 100)
             console.log('found....',skill);
   });
于 2012-10-18T05:00:55.817 回答