2016-04-21 113 views
1

我使用燼數據,我打電話給使用承諾的空白內容?

this.store.findAll('environment').then(function(values){ 
    //1 
},function(reason){ 
    //rejected 
}); 

我的API和我的代碼不會進入// 1,問題是,我得到這個對象,似乎是相當無效的。

enter image description here


這裏是我的API發回。

{ 
    "data": { 
    "environments": [ 
     { 
     "id": 1, 
     "localePath": "C:\\XML_DEPOT", 
     "name": "Acceptation 1", 
     "remotePath": "D:\\XML_DEPOT", 
     "databaseServerName": "blabla", 
     "databaseName": "blabla", 
     "port": 60903 
     }, 
     { 
     "id": 2, 
     "localePath": "bob", 
     "name": "Acceptation 2", 
     "remotePath": "bob", 
     "databaseServerName": "blabla\\blabla", 
     "databaseName": "blabla", 
     "port": 60904 
     } 
    ] 
    } 
} 

第二次嘗試我給了它,但仍然不喜歡它。

{ 
    "data": [ 
    { 
     "id": 1, 
     "localePath": "C:\\XML_DEPOT", 
     "name": "Acceptation 1", 
     "remotePath": "D:\\XML_DEPOT", 
     "databaseServerName": "W050A01SQL1", 
     "databaseName": "MAMROT01P1_MSCRM", 
     "port": 60903, 
     "type": "environments" 
    }, 
    { 
     "id": 2, 
     "localePath": "bob", 
     "name": "Acceptation 2", 
     "remotePath": "bob", 
     "databaseServerName": "W050A01SQL1\\W050A01SQL1B02", 
     "databaseName": "MAMROT01P1_MSCRM", 
     "port": 60904, 
     "type": "environments" 
    } 
    ] 
} 

這是我的模型聲明

//environment.js 
export default Model.extend({ 
    id: DS.attr('number'), 
    localePath: DS.attr('string'), 
    name: DS.attr('string'), 
    remotePath: DS.attr('string'), 
    databaseServerName: DS.attr('string'), 
    databaseName: DS.attr('string'), 
    port: DS.attr('number') 
}); 

謝謝!

+0

答案:將「數據」更改爲「環境」並刪除模型中的ID –

+0

是的,假定爲「id」。恭喜你弄明白了! –

回答

0

根據您使用的Ember版本,並假設您的環境模型設置良好(也許您可以列出,以便我們可以排除),您希望服務器將數據發回給您兩種格式之一,使灰燼數據開心:

  1. 灰燼1.13(或更早版本),其默認DS.RESTAdapterdocs

    { 
        "environments": [ 
        { 
         "id": 1, 
         "localePath": "C:\\XML_DEPOT", 
         "name": "Acceptation 1", 
         "remotePath": "D:\\XML_DEPOT", 
         "databaseServerName": "blabla", 
         "databaseName": "blabla", 
         "port": 60903 
        }, 
        { 
         "id": 2, 
         "localePath": "bob", 
         "name": "Acceptation 2", 
         "remotePath": "bob", 
         "databaseServerName": "blabla\\blabla", 
         "databaseName": "blabla", 
         "port": 60904 
        } 
        ] 
    } 
    
  2. 灰燼2.0+其默認DS.JSONAPIAdapterdocs

    { 
        "data": [ 
        { 
         "type": "environments", 
         "id": 1, 
         "attributes": { 
         "localePath": "C:\\XML_DEPOT", 
         "name": "Acceptation 1", 
         "remotePath": "D:\\XML_DEPOT", 
         "databaseServerName": "blabla", 
         "databaseName": "blabla", 
         "port": 60903 
         } 
        }, 
        { 
         "type": "environments", 
         "id": 2, 
         "attributes": { 
         "localePath": "bob", 
         "name": "Acceptation 2", 
         "remotePath": "bob", 
         "databaseServerName": "blabla\\blabla", 
         "databaseName": "blabla", 
         "port": 60904 
         } 
        } 
        ] 
    } 
    

而且模型可以刪除ID屬性,因爲你已經發現,因爲它是假設和灰燼爲你添加。

//environment.js 
export default Model.extend({ 
    localePath: DS.attr('string'), 
    name: DS.attr('string'), 
    remotePath: DS.attr('string'), 
    databaseServerName: DS.attr('string'), 
    databaseName: DS.attr('string'), 
    port: DS.attr('number') 
}); 

希望能得到你想要的東西。

+0

非常感謝! –

+0

我不明白從這個文檔http://emberjs.com/api/data/classes/DS.RESTAdapter.html 如果我使用RESTAdapter,我這樣做我發送我的JSON應該沒事嗎?這似乎不是從舊的文檔版本。 –

+0

@ Dr.DenisMcCracleJizz我認爲在使用RESTAdapter時,仍然需要刪除''data「:{wrapper}。它看起來仍然在尋找Ember 1.13風格的JSON。讓我知道這是否有效。 –