2012-10-18 66 views
2

我一直在调试这几天。 Store.findBy(function (record, id) 不行为。或者可能是我行为不端。将JSON放入代码中以便测试。 FindBy()匹配12345并且没有12345,它返回索引0和foo。我在做什么?Store.findBy()表现疯狂?

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

您是否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; 
      }); 

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

+0

通过这种方式,即使它存在于商店中,您也永远不会获得该值,因为'user .data.skills'是一个数组。 – MMT

+1

我知道,但是OP在问他为什么会得到误报,而不是他为什么找不到匹配。 – themel

+0

然后他会用true/false做什么,find的目的是检查是否它存在或不存在 – MMT

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

这是如何解释OP的问题? – themel

+0

对不起 - 我真的很糟糕的例子 - if(user.data.age === 71)return id; - 当71应该是第二条记录时,返回索引0和foo。 –

+0

对不起 - 没有看到“坚持合同评论。我正在从http://stackoverflow.com/questions/12444593/e​​xtjs-searching-deep-into-store-data/12483034?iemail=1#12483034例子...这个例子不正确吗?tia –