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一致しない場合は返されます。これは JavaScript で-1ある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 に答える