4

我有一个看起来像这样的 ember-data 模型定义:

Sylvius.Filter = DS.Model.extend({
  title: DS.attr('string'),
  slug: DS.attr('string'),
  // Belongs to Atlas
  atlas: DS.belongsTo('Sylvius.Atlas'),
  // Has images
  images: DS.hasMany('Sylvius.Image'),
  // May have AtlasExtras
  extras: DS.hasMany('Sylvius.AtlasExtra'),
  // Structures for this filter
  structures: DS.hasMany('Sylvius.Structure'),
  // This is the path to the thumbnails sprite.
  // Each image will have an index on this sprite
  thumbnailUrl: DS.attr('string'),
  // How big is each thumbnail?
  thumbnailHeight: DS.attr('number'),
  thumbnailWidth: DS.attr('number'),
  // How big are the images? 
  imageHeight: DS.attr('number'),
  // which image is selected?
  selectedImage: DS.belongsTo('Sylvius.Image')
});

我有一个像这样设置的 ember-data 固定适配器存储:

Sylvius.fixtureStore = DS.Store.create({
  revision: 4,
  adapter: DS.fixtureAdapter
});

...和看起来像这样的固定装置:

Sylvius.Filter.FIXTURES = [{
  "id": 1,
  "title": "Unlabeled",
  "slug": "unlabeled",
  "thumbnailUrl": "assets/img/surface_anatomy/photographic/srf-photo-unlabeled-tn.gif",
  "thumbnailWidth": 100,
  "thumbnailHeight": 75,
  "atlas_id": 1, 
  "images": [1, 2, 3, 4, 5, 6, 7],
  "structures": [0]
}];

(所有这些代码都在这个演示问题的 jsfiddle中。)

这是问题所在:标题可以很好地访问。蛞蝓也在那里。, thumbnailUrl, thumbnailWidth,thumbnailHeight都是未定义的。为什么?

4

1 回答 1

6

您没有遵循 ember-data 的以 rails 为中心的命名约定。您可以将夹具数据更改为:

{
  "id": 1,
  "title": "Dummy Title",
  "slug": "dummy-title",
  "thumbnail_url": "path/to/thumbnail.gif",
  "thumbnail_width": 100,
  "thumbnail_height": 75,
  "atlas_id": 1, 
  "images": [1, 2, 3, 4, 5, 6, 7],
  "structures": [0]
}

或更改您的映射以包含一个键:

thumbnailUrl: DS.attr('string', { key: 'thumbnailUrl' }),
thumbnailHeight: DS.attr('number', { key: 'thumbnailHeight' }),
thumbnailWidth: DS.attr('number', { key: 'thumbnailWidth' })
于 2012-07-13T16:18:15.653 回答