余烬模型未在模板中显示字段数据



app/models/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()    
});

app/template/index.hbs

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

我从 sinatra 返回此数据请求/rentals 数据请求

  { 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:

使用 ember 2.5

> 根据dynamic_cast的评论,我将 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

最新更新