0

This is the same question as this one or this one, but it seems that the code generated by ember-cli has changed. This is my current system:

» ember --version
version: 0.1.2
node: 0.10.25
npm: 2.1.3

This is the full content of my book model file (models/book.js):

import DS from 'ember-data';

export default DS.Model.extend({
    'title': DS.attr('string'),
    'author': DS.attr('string'),

});

The replies to the other questions and this post tell me to use reopenClass, but with the current format of the model file there is nothing for me to reopen.

I have tried doing:

import DS from 'ember-data';

export default DS.Model.extend({
    'title': DS.attr('string'),
    'author': DS.attr('string'),
    FIXTURES : [
        {
            id: 1,
            title: "Protector",
            author: "Larry Nieven"
        },
        {
            id: 2,
            title: "The greatest book ever",
            author: "Juan"
        }
    ]
});

But no luck. How can I define fixtures?

Community
  • 1
  • 1
blueFast
  • 41,341
  • 63
  • 198
  • 344
  • 1
    possible duplicate of [Ember fixtures not working](http://stackoverflow.com/questions/25744233/ember-fixtures-not-working) – MilkyWayJoe Oct 17 '14 at 15:48

1 Answers1

2

Convert your model class to the following should do the trick. Store the model in a variable, reopen the class and add the fixtures, then export the model.

import DS from 'ember-data';

var BookModel = DS.Model.extend({
    'title': DS.attr('string'),
    'author': DS.attr('string')
});

BookModel.reopenClass({
    FIXTURES : [
        {
            id: 1,
            title: "Protector",
            author: "Larry Nieven"
        },
        {
            id: 2,
            title: "The greatest book ever",
            author: "Juan"
        }
    ]
});

export default BookModel;
ToddSmithSalter
  • 715
  • 6
  • 20
  • Not working. No matter if I call it BookModel, or just Book, or anything. Also tried with `BookModel.FIXTURES` syntax, and playing around with `Ember.MODEL_FACTORY_INJECTIONS`, but nothing works. I have defined an `app/adapters/application.js` fixture adapter (explained nowhere, but I found an example in the ember-todo, which is working - but only with an old version of ember-cli (ember?), and not with the current ember-cli (ember?) version). There is no official guide on how to setup fixtures with ember-cli. And I only want to use fixtures to reproduce another (more complex) problem ... – blueFast Oct 20 '14 at 10:42
  • 2
    Now it is working. I was missing the model hook for the route, with its corresponding find. Difficult to hit a fast moving target, specially taking into account that I also make my usual programming mistakes, and that I am unable to distinguish between the fact that there is something I do *not* know (`reopenClass`) and something I know but I am doing wrong (missing adapter and model) – blueFast Oct 20 '14 at 11:05
  • Glad you figured it out. It is nice to be able to change the adapter type on a per-resource basis, but not having all your ducks in a row will bite you. – ToddSmithSalter Oct 20 '14 at 22:29