2013-07-12 1 views
0
"content":{"ups_ground": 
       {"amount":"7.06", 
       "currency_code":"USD", 
       "data":[], 
       "tnt":"Monday, 7\/15 at 11:00pm"}, 

      "ups_next_day_air_saver": 
       {"amount":"26.44", 
        "currency_code":"USD", 
        "data":[], 
        "tnt":"Monday, 7\/15 at 3:00pm"}, 

      "ups_next_day_early_a.m.": 
       {"amount":"63.84", 
       "currency_code":"USD", 
       "data":[], 
       "tnt":"Monday, 7\/15 at 8:30am"}, 

      "ups_next_day_air": 
        {"amount":"30.99", 
         "currency_code":"USD", 
         "data":[],"tnt": 
         "Monday, 7\/15 at 10:30am"} 
    } 
} 

나는 위와 같이 JSON을 중첩하고 정말 가게 루트 속성을 작성하는 모델은 사람이json을 중첩했습니다. 모델 및 상점을 작성하는 방법 ?? 내용 만 : 나는 rootProperty를주는 시도 엽차 터치 2

에 저를 도와주세요 fields..could 방법에 혼란스러워했다 작동하지 않습니다.

{ups_ground.amount}와 (과) 같은 방식으로 데이터를 호출하고 싶습니다. ??

답변

0

서버 구조를 이와 같은 표준 extjs json 저장소 형식으로 변경할 수 있습니까?

{ 
    "content":[ 
     { 
      "shipping_type":"ups_ground", 
      "amount":"7.06", 
      "currency_code":"USD", 
      "data":[], 
      "tnt":"Monday, 7\/15 at 11:00pm" 
     }, 
     { 
      "shipping_type":"ups_next_day_air_saver", 
      "amount":"26.44", 
      "currency_code":"USD", 
      "data":[], 
      "tnt":"Monday, 7\/15 at 3:00pm" 
     }, 
     { 
      "shipping_type":"ups_next_day_early_a.m.", 
      "amount":"63.84", 
      "currency_code":"USD", 
      "data":[], 
      "tnt":"Monday, 7\/15 at 8:30am" 
     }, 
     { 
      "shipping_type":"ups_next_day_air", 
      "amount":"30.99", 
      "currency_code":"USD", 
      "data":[],"tnt": 
      "Monday, 7\/15 at 10:30am" 
     } 
    ] 
} 

그리고 당신은 모델을 선언하고 다음과 같이 저장할 수 : 그렇다면, 상점 설치가 정말 쉬운 것입니다

Ext.define("ShippingModel", { 
    extend: "Ext.data.Model", 
    fields: [ 
     {name:"shipping_type", type:"string"}, 
     {name:"amount",  type:"float"}, 
     {name:"currency_code", type:"string"}, 
     {name:"data",   type:"string"}, 
     {name:"tnt",   type:"string"}//this may need to be a date, lookup date format for this yourself 
    ] 
}); 

Ext.create('Ext.data.Store', { 
    model: 'ShippingModel', 
    controller: 'news', 
    proxy: { 
     type: 'ajax', 
     url: 'your url', 
     reader: { 
      type: 'json', 
      root: 'content' 
     }, 
     extraParams:{ 
      action:'getShippingRates'//you may not need the extraParams, just putting here for example 
     } 
    }, 
    autoLoad: true 
}); 

당신은 그런 그 표준을 할 수없는 경우, 당신은을 확장 할 수 있습니다 reader 클래스를 사용하여 사용자 정의 데이터 구조를 읽을 수 있습니다. 그것은 다음과 같이 보일 것입니다 :

Ext.define("MyWeirdlyStucturedDataReader", { 
    extend: "Ext.data.reader.Json", 

    extractData: function(root){ 

     var newStructure = []; 

     for(var shippingType in root) 
     { 
      var newRecord = root[shippingType]; 
      newRecord.shipping_type = shippingType; 
      newStructure.push(newRecord); 
     } 

     return this.callParent(newStructure); 

    } 

}); 

은 그럼 당신은 'MyWeirdlyStucturedDataReader'에 프록시의 리더 유형의 속성을 설정합니다.

*이 코드는 모두 테스트되지 않았으므로 작동하지 않는 경우에도 수행 할 작업에 대한 아이디어를 얻을 수 있습니다.

+0

감사합니다 ... json ..을 변경했습니다. – GiGi

관련 문제