迭代模板中的fixture



我使用ember-cliEmber v1.13.0-beta.1,我有以下模型,路由和模板。

模型/products.js

import DS from 'ember-data';
var Product = DS.Model.extend({
  title: DS.attr('string'),
  ...
});
Product.reopenClass({
  FIXTURES: [
    {
      id: 1,
      title: 'Title',
      ...
    }
  ]
});
export default Product;

路线/products.js

import Ember from 'ember';
export default Ember.Route.extend({
  model: function() {
    return this.store.all('product');
  }
});
<<p> 模板/产品/strong>
{{#each product in model}}
  {{product.title}}
{{/each}}

同样在app/js我有App.ApplicationAdapter = DS.FixtureAdapter;

当我在路由中使用一个简单的数组时,它在模板中呈现得很好,但自从我切换到fixture后,它就不再这样做了。

当我记录this.store.all('product')时,它似乎返回正确的对象。

也不确定是否相关但从一开始我就不能做

{{#each}}
  {{title}}
{{/each}}

我必须总是像上面那样引用模型

ember-cli应用程序中的fixture工作方式不同。

如果你习惯使用fixture在你的应用程序中获取测试数据开发时,您将无法像以前那样创建fixture数据做(即按照指南的规定)。这是因为模型在你的余烬CLI应用程序(像所有其他对象)不附加到全局名称空间。

为产品API端点创建模拟,使用

ember g http-mock products

你的app/adapters/application.js应该是

 // adapters/application.js
 import DS from "ember-data";
 export default DS.FixtureAdapter.extend({});

看到http://www.ember-cli.com/mocks-and-fixtures

最新更新