ember.js - Ember-data 夹具适配器未加载所有数据

标签 ember.js ember-data

我有一个 looks like this 的 Ember 数据模型定义:

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 fixture-adapter store:
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]
}];

(所有这些代码都在 this jsfiddle 中,它演示了这个问题。)

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

最佳答案

您没有遵循 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' })

关于ember.js - Ember-data 夹具适配器未加载所有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11470545/

相关文章:

ember.js - 如何在另一个 Controller Ember 中绑定(bind)应用程序 Controller 属性

ember.js - Ember 错误 "Uncaught TypeError: Cannot read property ' typeKey' of undefined "on model save

javascript - ember linkTo 在路由的模型 Hook 中发送空参数

javascript - Ember js 将 id 传递给绑定(bind)属性

ember.js - 我可以访问 {{#with}} 范围内的 View 属性吗?

ember.js - Ember 数据 : A data hash was loaded . .. 但没有提供主键 'undefined'

javascript - 如何在ember js中以一对多关系将父对象添加到子对象

ember.js - 在 Ember : Fixture vs REST adapter 中预加载数据

ember.js - 嵌入的Ember数据记录当前状态?

ember.js - Ember 与 ember-data 模型的集成测试