Where to place fixtures?

拟墨画扇 提交于 2019-12-10 02:56:23

问题


Where should I define fixtures in an Ember JS app generated with ember-cli? I've tried numerous places, such as app.js and within a folder called "fixtures".


回答1:


After digging around I discovered that changing Ember.MODEL_FACTORY_INJECTIONS = true; in the file app.js to Ember.MODEL_FACTORY_INJECTIONS = false; is solving the problem.

Through this question I also found another solution where you don't have to change the configuration:

Instead of defining the fixtures as described you have to use reopenClass:

//models/item.js
var Item = DS.Model.extend({...});

Item.reopenClass({
  FIXTURES: [
    { id: 1, ... },
    { id: 2, ... }
  ]
});

export default Item

Happy developing with Ember and ember-cli :-)




回答2:


Instead of using fixtures, the way I'm doing it in Ember CLI 0.0.40 is generating api stubs.

ember generate api-stub tasks

I'm a node.js beginner, but from the looks of it, it sets up an express server script to respond to the /tasks endpoint, corresponding to the name you pass to the command, in the format the Ember REST adapter is expecting. Then you simply fill in the empty array with your fixture data. Easy peesy!

The benefit I see is that I won't have to rework anything later to integrate with a restful api getting me one step closer to launching a real app some day.

This generator is not thoroughly documented yet. It's only displayed as an item in the ember help generate command, which I was brave/desperate/curious/stupid enough to try.




回答3:


If you use findQuery to get your data you will get this error when using the method above:

Error while loading route: Error: Assertion Failed: Not implemented: You must override the DS.FixtureAdapter::queryFixtures method to support querying the fixture store.

To fix this I created an adapter for the model and implemented the queryFixtures method to return the fixtures.

#/adapters/[model-name].js 

queryFixtures: function() {
    return [ {
        "key" : "value",
     },
     {
        "key" : "value",
     },
     etc...
     ]
   };

I had to do this in addition to reopenClass in my model definition as stated above. In fact it was the same data, I cut and pasted. This smells a little bad, but it works. I'm sure there's a better way to do this without duplicating the fixtures, I just don't know what it is.




回答4:


I define it in the model folder

 //models/item.js
 var Item = DS.Model.extend({...})

 Item.FIXTURES = [
        {
            id:1,
            ...
         }
 ];

 export default Item


来源:https://stackoverflow.com/questions/22773798/where-to-place-fixtures

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