2014-07-15 4 views
0

여러 중첩 수준이 포함 된 JSON을로드하는 데 문제가 있습니다. 각 모델과 연관된 일련의 모델 (항목)이있는 모델 (위치)이 있습니다. WO 이상적인에서Sencha Touch에서 여러 개의 중첩 JSON 자동로드

항목 모델

Ext.define('SenchaSandbox.model.Item', { 
    extend: 'Ext.data.Model', 

    requires: ['Ext.data.Field'], 

    config: { 
     fields: ['id', 'name'] 
    } 
}); 

위치 모델

Ext.define('SenchaSandbox.model.Location', { 
    extend: 'Ext.data.Model', 

    requires: ['Ext.data.Field', 'Ext.data.association.HasMany'], 

    uses: ['SenchaSandbox.model.Item'], 

    config: { 
     fields: ['id', 'name'], 
     associations: [{ 
      type: 'hasMany', 
      model: 'SenchaSandbox.model.Item', 
      autoLoad: true, 
      name: 'items' 
     }] 
    } 
}); 

위치 저장

Ext.define('SenchaSandbox.store.LocationStore', { 
    extend: 'Ext.data.Store', 

    requires: [ 
     'Location', 
     'Ext.data.proxy.Ajax', 
     'Ext.data.reader.Json' 
    ], 

    config: { 
     model: 'SenchaSandbox.model.Location', 
     storeId: 'LocationStore', 
     proxy: { 
      type: 'ajax', 
      url: 'DummyGood.json', 
      reader: { 
       type: 'json', 
       rootProperty: 'location' 
      } 
     } 
    } 
}); 

rld, 서버의 JSON이 다음과 같은 경우 Sencha는 위치를로드하고 관련 항목을 자동으로로드합니다.

좋은 JSON은

{"location": [ 
    { 
     "id": 100, 
     "name": "Location 1", 
     "items": [ 
      { 
       "id": 1, 
       "name": "Item A" 
      }, 
      { 
       "id": 2, 
       "name": "Item B" 
      } 
     ] 
    }, 
    { 
     "id": 200, 
     "name": "Location 2", 
     "items": [ 
      { 
       "id": 3, 
       "name": "Item A" 
      }, 
      { 
       "id": 4, 
       "name": "Item C" 
      } 
     ] 
    } 
]} 

그러나 서버는 실제로 다음과 같은 몇 가지 추가 중첩와 JSON을 반환합니다.

나쁜 (서버) JSON

{"CollectionLocation": { 
    "collection": [ 
     { 
      "id": 100, 
      "name": "Location 1", 
      "items": { 
       "collection": [ 
        { 
         "id": 1, 
         "name": "Item A" 
        }, 
        { 
         "id": 2, 
         "name": "Item B" 
        } 
       ] 
      } 
     }, 
     { 
      "id": 200, 
      "name": "Location 2", 
      "items": { 
       "collection": [ 
        { 
         "id": 3, 
         "name": "Item A" 
        }, 
        { 
         "id": 4, 
         "name": "Item C" 
        } 
       ] 
      } 
     } 
    ] 
}} 

나는이 JSON을 생성하는 서버 코드를 통제 할 수 없다고 가정,로드 내 옵션 깨끗한 방식으로이 데이터 무엇인가? Sencha의 자동로드 기능을 계속 활용할 수 있습니까? 아니면 하위 저장소를 만들고 하위 모델을 채우는 코드를 직접 작성해야합니까?

필자는 누구나 샘플을 뒤섞을 수있는 경우를 대비하여 https://fiddle.sencha.com/#fiddle/7n6 피들을 작성했습니다.

답변