2016-06-10 92 views
-1

應用程序/模型/ index.js灰燼模型模板不顯示字段數據

import Model from 'ember-data/model'; 
import attr from 'ember-data/attr'; 

export default Model.extend({ 
    title: attr(), 
    owner: attr(), 
    city: attr(), 
    type: attr(), 
    image: attr(), 
    bedrooms: attr()  
}); 

應用/模板/ index.hbs

{{#each model as |rental|}} 
    <p>Location: {{rental.city}}</p> 
    <p>Number of bedrooms: {{rental.bedrooms}}</p> 
{{/each}} 

是從西納特拉的/租金數據請求返回此

{ data: [{ 
    id: 1, 
    title: 'Grand Old Mansion', 
    owner: 'Veruca Salt', 
    city: 'San Francisco', 
    bedrooms: 15, 
    type: 'rental', 
    image:  'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg' 
}, { 
    id: 2, 
    title: 'Urban Living', 
    owner: 'Mike TV', 
    city: 'Seattle', 
    bedrooms: 1, 
    type: 'rental', 
    image:  'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_T egucigalpa.jpg' 
}, { 
    id: 3, 
    title: 'Downtown Charm', 
    owner: 'Violet Beauregarde', 
    city: 'Portland', 
    bedrooms: 3, 
    type: 'rental', 
    image:  'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Bu ilding_-_Portland_Oregon.jpg' 
}, { 
    id: 4, 
    title: 'xDowntown Charm', 
    owner: 'Violet Beauregarde', 
    city: 'Portland', 
    bedrooms: 3, 
    type: 'rental', 
    image:  'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg' 
}]}.to_json 

每個循環知道有多少條記錄,但是由於瀏覽器顯示這個數據缺少字段數據

Location: 
Number of bedrooms: 
Location: 
Number of bedrooms: 
Location: 
Number of bedrooms: 
Location: 
Number of bedrooms: 

通過dynamic_cast的使用燼2.5

+0

您的服務器返回的JSON不是有效的JSON API。你在Ember中使用自定義序列化程序嗎? –

+0

謝謝!我在猜測JSON結構,我搜索了JSON API並使用了我發現的例子來糾正結構並使其工作。 – blizz

回答

1

根據註釋,我改變了JSON結構這一點,並得到它的工作。

{ 
    "data" => [{ 
    "type" => "rentals", 
    "id" => "1", 
    "attributes" => { 
     "title" => 'Grand Old Mansion', 
     "owner" => 'Veruca Salt', 
     "city" => 'San Francisco', 
     "bedrooms" => 15, 
     "type" => 'rental', 
     "image" =>  'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg' 
     } 
    }, 
    { 
    "type" => "rentals", 
    "id" => "2", 
    "attributes" => { 
     "title" => 'Urban Living', 
     "owner" => 'Mike TV', 
     "city" => 'Seattle', 
     "bedrooms" => 1, 
     "type" => 'rental', 
     "image" =>  'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_T egucigalpa.jpg' 
     } 
    }, 
    { 
    "type" => "rentals", 
    "id" => "3", 
    "attributes" => { 
     "title" => 'Downtown Charm', 
     "owner" => 'Violet Beauregarde', 
     "city" => 'Portland', 
     "type" => 'Apartment', 
     "bedrooms" => 3, 
     "image" => 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg' 
     } 
    } 
    ] 
}.to_json