Ember-data fixtures adapter not loading all data

为君一笑 提交于 2020-01-01 12:11:09

问题


I have an ember-data model definition that looks like this:

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')
});

I have an ember-data fixture-adapter store set up like this:

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

...and fixtures which look like this:

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]
}];

(All this code is in this jsfiddle which demonstrates the problem.)

Here's the issue: the title is accessible just fine. The slug is also there. The thumbnailUrl, thumbnailWidth, thumbnailHeight, are all undefined. Why?


回答1:


You are not following ember-data's rails centric naming conventions. You can either change your fixture data to:

{
  "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]
}

or change your mapping to include a key:

thumbnailUrl: DS.attr('string', { key: 'thumbnailUrl' }),
thumbnailHeight: DS.attr('number', { key: 'thumbnailHeight' }),
thumbnailWidth: DS.attr('number', { key: 'thumbnailWidth' })


来源:https://stackoverflow.com/questions/11470545/ember-data-fixtures-adapter-not-loading-all-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!