Store.findBy()表现得很疯狂?

时间:2012-10-18 03:59:46

标签: extjs

我已经调试了好几天了。 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');
                }
            }
        });
    }
}); 

2 个答案:

答案 0 :(得分:4)

你有read the documentation吗?您的findBy方法不符合合同。特别是,如果它不匹配,则会返回-1,因为这是JavaScript,-1true,所以找到第一条记录。

        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;
            });

(这解决了你的误报问题,我并没有声称你的支票会找到任何东西)。

答案 1 :(得分:-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);
   });
相关问题