PericlesTheo
PericlesTheo

Reputation: 2469

Iterate through fixtures in a template

I'm using ember-cli and Ember v1.13.0-beta.1 and I have the following model, route and template.

models/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;

routes/products.js

import Ember from 'ember';

export default Ember.Route.extend({
  model: function() {
    return this.store.all('product');
  }
});

templates/products

{{#each product in model}}
  {{product.title}}
{{/each}}

Also in app/js I have App.ApplicationAdapter = DS.FixtureAdapter;

When I use a simple Array in the route, then it renders fine in the template but ever since I switched to fixtures, it stopped doing so.

When I log this.store.all('product') it seems to return the right object.

Also, not sure if it's relevant but from the beginning I cannot do

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

I have to always reference the model like above

Upvotes: 1

Views: 51

Answers (1)

Asgaroth
Asgaroth

Reputation: 4334

Fixtures in ember-cli applications work differently.

If you’re used to using fixtures to get test data into your app during development, you won’t be able to create fixture data like you’re used to doing (i.e. as specified in the guides). This is because the models in your Ember CLI app (like all other objects) aren’t attached to the global namespace.

To create a mock for a products API endpoint, use

ember g http-mock products

Your app/adapters/application.js should be

 // adapters/application.js
 import DS from "ember-data";

 export default DS.FixtureAdapter.extend({});

See http://www.ember-cli.com/#mocks-and-fixtures

Upvotes: 1

Related Questions