2013-02-28 112 views
1

從各個位置獲取衝突的信息是否嵌套的json在extjs中可用。 NB使用Sencha架構師可能會使事情複雜...extjs嵌套json

我有一個客戶端json對象正在通過商店/ ajax代理返回。我無法訪問超出第一級的任何內容。

JSON看起來是這樣的:

{ 
    "client": { 
     "0": { 
      "id": "12345", 
      "type_id": "3", 
      "association_id": "0", 
      "is_active": "1", 
      "manager_id": "2", 
      "business_name": null, 
      "has_payroll": "0", 
      "business_proprietor_contact_id": null, 
      "date_created": "1358893243", 
      "date_modified": "1358893243" 
     }, 
     "contacts": [ 
      { 
       "id": "1", 
       "client_id": "12345", 
       "contact_id": "1", 
       "is_primary": "0", 
       "display_priority": "0", 
       "relationship": "Client", 
       "friendly_name": "Homer", 
       "contact": [ 
        { 
         "id": "1", 
         "surname": "Mr.", 
         "first_name": "Homer", 
         "middle_name": "J", 
         "last_name": "Simpson", 
         "client_id": "12345", 
         "date_created": "1356038714", 
         "date_modified": null 
        }, 
        { 
         "addresses": [ 
          { 
           "id": "1", 
           "contact_id": "1", 
           "address_id": "16", 
           "is_mailing_address": "0", 
           "is_primary_address": "0", 
           "display_priority": "0", 
           "address": { 
            "0": { 
             "id": "16", 
             "address": "5 Dirt Rd", 
             "address2": "", 
             "city": "TAXMASTER", 
             "state": "NY", 
             "zip_code": "454545", 
             "country": "USA", 
             "other": null, 
             "phone": null, 
             "fax": null, 
             "display_priority": null, 
             "is_mailing_address": "1" 
            }, 
            "data": [ 
             { 
              "id": "10", 
              "label": "Phone", 
              "value": "BUT WHO WAS", 
              "date_created": "1356104685", 
              "date_modified": "1357142782", 
              "user_id": null, 
              "display_priority": null, 
              "client_id": null, 
              "contact_id": null, 
              "account_id": null, 
              "business_id": null, 
              "address_id": "16", 
              "is_deleted": "0" 
             }, 
             { 
              "id": "14", 
              "label": "Fax", 
              "value": "simile", 
              "date_created": "1356557379", 
              "date_modified": "1357142782", 
              "user_id": null, 
              "display_priority": null, 
              "client_id": null, 
              "contact_id": null, 
              "account_id": null, 
              "business_id": null, 
              "address_id": "16", 
              "is_deleted": "0" 
             }, 
             { 
              "id": "17", 
              "label": "Also Known as:", 
              "value": "DUDE", 
              "date_created": "1357141357", 
              "date_modified": "1357142782", 
              "user_id": null, 
              "display_priority": null, 
              "client_id": null, 
              "contact_id": null, 
              "account_id": null, 
              "business_id": null, 
              "address_id": "16", 
              "is_deleted": "0" 
             } 
            ] 
           } 
          }, 
          { 
           "id": "6", 
           "contact_id": "1", 
           "address_id": "26", 
           "is_mailing_address": "0", 
           "is_primary_address": "0", 
           "display_priority": "0", 
           "address": { 
            "0": { 
             "id": "26", 
             "address": "1 Lift Road", 
             "address2": "", 
             "city": "Tony Beach", 
             "state": "KS", 
             "zip_code": "15151", 
             "country": "USA", 
             "other": null, 
             "phone": null, 
             "fax": null, 
             "display_priority": null, 
             "is_mailing_address": "0" 
            }, 
            "data": [ 
             { 
              "id": "18", 
              "label": "Phone:", 
              "value": "cool phone number", 
              "date_created": "1357141527", 
              "date_modified": "1357141527", 
              "user_id": null, 
              "display_priority": null, 
              "client_id": null, 
              "contact_id": null, 
              "account_id": null, 
              "business_id": null, 
              "address_id": "26", 
              "is_deleted": "0" 
             }, 
             { 
              "id": "19", 
              "label": "Note:", 
              "value": "cool note", 
              "date_created": "1357141527", 
              "date_modified": "1357141527", 
              "user_id": null, 
              "display_priority": null, 
              "client_id": null, 
              "contact_id": null, 
              "account_id": null, 
              "business_id": null, 
              "address_id": "26", 
              "is_deleted": "0" 
             } 
            ] 
           } 
          }, 
         ], 
         "data": [ 
          { 
           "id": "1", 
           "label": "asdf", 
           "value": "dfdfefefefef", 
           "date_created": "1356038714", 
           "date_modified": "1356040822", 
           "user_id": "2", 
           "display_priority": "0", 
           "client_id": null, 
           "contact_id": "1", 
           "account_id": null, 
           "business_id": null, 
           "address_id": null, 
           "is_deleted": "0" 
          }, 
          { 
           "id": "2", 
           "label": "asfd", 
           "value": "152151", 
           "date_created": "1356038714", 
           "date_modified": "1356040822", 
           "user_id": null, 
           "display_priority": "2", 
           "client_id": null, 
           "contact_id": "1", 
           "account_id": null, 
           "business_id": null, 
           "address_id": null, 
           "is_deleted": "0" 
          }, 
         ] 
        } 
       ] 
      }, 
      { 
       "id": "7", 
       "client_id": "12345", 
       "contact_id": "11", 
       "is_primary": "0", 
       "display_priority": "0", 
       "relationship": "None", 
       "friendly_name": "Mary Jane", 
       "contact": { 
        "0": { 
         "id": "11", 
         "surname": "", 
         "first_name": "Mary Jane", 
         "middle_name": "", 
         "last_name": "Simpson", 
         "client_id": null, 
         "date_created": "1357145795", 
         "date_modified": null 
        }, 
        "7": { 
         "addresses": [ 
          { 
           "id": "11", 
           "contact_id": "11", 
           "address_id": "33", 
           "is_mailing_address": "0", 
           "is_primary_address": "0", 
           "display_priority": "0", 
           "address": { 
            "0": { 
             "id": "33", 
             "address": "33 Street", 
             "address2": "", 
             "city": "NY", 
             "state": "NY", 
             "zip_code": "06830", 
             "country": "USA", 
             "other": null, 
             "phone": null, 
             "fax": null, 
             "display_priority": null, 
             "is_mailing_address": "0" 
            }, 
            "data": [ 
             { 
              "id": "31", 
              "label": "Fax:", 
              "value": "asdfasdf", 
              "date_created": "1357145905", 
              "date_modified": "1357145939", 
              "user_id": null, 
              "display_priority": null, 
              "client_id": null, 
              "contact_id": null, 
              "account_id": null, 
              "business_id": null, 
              "address_id": "33", 
              "is_deleted": "0" 
             }, 
             { 
              "id": "32", 
              "label": "Fax:", 
              "value": "sadfsadf", 
              "date_created": "1357145905", 
              "date_modified": "1357145905", 
              "user_id": null, 
              "display_priority": null, 
              "client_id": null, 
              "contact_id": null, 
              "account_id": null, 
              "business_id": null, 
              "address_id": "33", 
              "is_deleted": "0" 
             } 
            ] 
           } 
          } 
         ], 
         "data": [ 
          { 
           "id": "29", 
           "label": "Phone:", 
           "value": "asdfsadf", 
           "date_created": "1357145833", 
           "date_modified": "1357145833", 
           "user_id": "2", 
           "display_priority": null, 
           "client_id": null, 
           "contact_id": "11", 
           "account_id": null, 
           "business_id": null, 
           "address_id": null, 
           "is_deleted": "0" 
          }, 
          { 
           "id": "30", 
           "label": "Email:", 
           "value": "dsfgdsfg", 
           "date_created": "1357145855", 
           "date_modified": "1357145855", 
           "user_id": "2", 
           "display_priority": null, 
           "client_id": null, 
           "contact_id": "11", 
           "account_id": null, 
           "business_id": null, 
           "address_id": null, 
           "is_deleted": "0" 
          } 
         ] 
        } 
       } 
      } 
     ], 
     "notes": [ 
      { 
       "id": "5", 
       "client_id": "12345", 
       "label": "This is a HomerHomer Note", 
       "value": "This is from Homer's Dashboard Notes.", 
       "contact_id": null, 
       "address_id": null, 
       "account_id": null, 
       "account_data_id": null, 
       "account_credential_id": null, 
       "payment_id": null, 
       "contact_log_id": null, 
       "user_id": "2", 
       "bill_id": null, 
       "is_deleted": "0", 
       "date_created": "1357148256", 
       "date_modified": "1357148256" 
      } 
     ] 
    } 
} 

根據https://stackoverflow.com/a/8318589/183254嵌套JSON不可用。根據https://stackoverflow.com/a/5604594/183254可用,但必須在一定的格式。

我做了基於API文檔格式的小測試:

{ 
    results: 2000, 
    rows: [ 
     { id: 1, firstname: 'Bill', occupation: 'Gardener', cool: {neat: 'wow'} }, 
     { id: 2, firstname: 'Ben' , occupation: 'Horticulturalist' }, 
    ] 
} 

與此進來,我可以調用一切從一個第三方物流。特別是,cool.neat打印wow。但是,將cool更改爲cool:[{neat:'wow}]休息。所以看起來嵌套json部分工作 - 可能只是部分原因,因爲我做錯了什麼。

我的想法是,對api執行單個請求並收集所有需要的信息,解析它,並將它發送給組件,而不是爲每個需要的數據組創建代理,這樣纔有意義。我可能是錯的...

除了必要的指導/ facepunches我有2個問題:

1)是否有可能/實際嘗試做我在做什麼?即試圖使用單個請求來收集多個組件的數據?

2)有沒有辦法從tpl調試數據?

這是extjs 4.1通過Sencha建築師

謝謝。

回答

1

我有這個相同的問題。我可以解決這個問題的唯一方法(我嘗試了映射和其他幾個解決方案)基本上都有一個主存儲和多個子存儲。從ajax獲取信息並將其帶入主商店。有一個「加載」監聽器,當它加載時,檢查是否需要將信息傳遞給其他商店。

將所有的substores autoload屬性設置爲false並且永不加載它們。

即:

load: function(t,records,successful,eOpts) { 
    var substores [{store: nameOfStore, root: rootPropInTheJson}, 
        {store...etc...etc..]; 
    for (var i = 0; i < substores.length; i++) { 
     if (pathtoroot* != undefined) { 
      substores[i].store.add(pathtojsonroot) 
     } 
    } 
} 

至少,這是我認爲你的意思。如果這就是你的意思,那麼是的,我發現它很實用,我不知道第二個問題的答案。

+0

我假設你有其他的變量集? 'pathtoroot *'?錯字?無論如何,我總是收到'Uncaught TypeError:Object mySubStore has no method'add''。另外,你可以從主存儲器的構造函數中的加載監聽器調用它。謝謝! – stormdrain 2013-02-28 20:19:01

+0

隨機變量只是填充。你把var的名字和你的商店放在那裏。如果您正確地做到了這一點,那麼在商店中使用添加變量將只爲其添加記錄。所以你基本上最終確定是否需要添加記錄,然後在需要時將其推出。你可以發佈給你類型錯誤的代碼,以及商店聲明。是的,它是在主店負載監聽器 – 2013-03-01 11:47:08

+0

而*是因爲我打算在底部添加一個註釋,提到根的路徑最終會變成如下形式: t.data.items [i] .raw [substores [I] .root] [j]的。 另一件需要注意的事情是,您可能不是尋找根,而是尋找根內的值。也許是一個ID? t.data.items [0] .raw [substores [i] .root] [j] .id – 2013-03-01 12:10:47