Ember fixtures not working

懵懂的女人 提交于 2020-01-23 18:38:09

问题


This is my code (using ember-cli):

app.coffee

`import Ember from 'ember'`
`import Resolver from 'ember/resolver'`
`import loadInitializers from 'ember/load-initializers'`

Ember.MODEL_FACTORY_INJECTIONS = true

App = Ember.Application.extend
  modulePrefix: 'dashboard' # TODO: loaded via config
  Resolver: Resolver


loadInitializers App, 'dashboard'

`export default App`

adapters/application.coffee

`import DS from 'ember-data'`

ApplicationAdapter = DS.FixtureAdapter.extend()

`export default ApplicationAdapter`

models/mod.coffee

`import DS from 'ember-data'`

Mod = DS.Model.extend 
  name: DS.attr 'string'
  body: DS.attr 'string'
  summary: DS.attr 'string'
  category: DS.attr 'string'

Mod.reopenClass {
  FIXTURES: [
    {
      id: 1
      name: "First mod"
      body: "..."
      summary: "..."
      category: "api"
    },
    {
      id: 2
      name: "Second mod"
      body: "..."
      summary: "..."
      category: "api"
    }
  ]
}

`export default Mod`

But in the app nothing is thrown when I run ember serve nor in the browser conole (output:

DEBUG: ------------------------------- vendor.js:27630
DEBUG: Ember      : 1.7.0 vendor.js:27630
DEBUG: Ember Data : 1.0.0-beta.8.2a68c63a vendor.js:27630
DEBUG: Handlebars : 1.3.0 vendor.js:27630
DEBUG: jQuery     : 1.11.1 vendor.js:27630
DEBUG: ------------------------------- vendor.js:27630
generated -> route:application Object {fullName: "route:application"} vendor.js:27630
generated -> route:index Object {fullName: "route:index"} vendor.js:27630
generated -> controller:application Object {fullName: "controller:application"} vendor.js:27630
Rendering application with default view <dashboard@view:toplevel::ember323> Object {fullName: "view:application"} vendor.js:27630
generated -> controller:index Object {fullName: "controller:index"} vendor.js:27630
Rendering index with <dashboard@view:index::ember339> Object {fullName: "view:index"} vendor.js:27630
Ember Debugger Active 

)

In ember inspector it shows me my mod model but with no record

Here is the whole github repo https://github.com/OpenCubes/dashboard/tree/temp


回答1:


ok 2 things.

first you must generate a route if you are fetching the whole collection. Our actively generated routes don't do this by default. I believe it just has to do with ambiguity and that we don't want to fetch your entire datasource by accident, so we leave this up to the user todo.

// app/routes/mods.js
import Ember from 'ember';
export default Ember.Route.extend({
  model: function() { return this.store.find('mod'); }
});

second you must use reopenClass when providing fixtures. This is because IE6 + there is not way to properly propagate static/class variables to descendants. So ember implements its own mechanism. At some future point in time setPrototypeOf will be usable and we can defer to that. Until then for ember-cli apps, please use embers sanctioned way.

import DS from 'ember-data';
var Mod = DS.Model.extend({...})
Mod.reopenClass({
  FIXTURES: [
  ...
  ]
});
export default Mod;

then it works




回答2:


Update

Hi, I have moved that code to cli and basically, all I had to do was:

create an adapter for Mod:

C:\>ember g adapter mod

Then I went to the source of the generated Mod adapter, and changed the declaration to extend from FixtureAdapter instead of RESTAdapter.

import DS from 'ember-data';

export default DS.FixtureAdapter.extend();

Also, had to change the model declaration, and it's pretty much the same as yours now. No need to create store or anything.


I don't know really if this is why you're having trouble, but, did you override the adapter in your store? I suspect it might not be using your adapter.

I've made a quick sample (here) that you can use as reference.

Note in the sample below that I'm passing the fixture adapter name as a string to the store (there are other ways to do this as well).

App.ApplicationAdapter = DS.FixtureAdapter.extend();

App.Store = DS.Store.extend({
  // override default adapter
  adapter: 'ApplicationAdapter'
});

[ ... other code ... ]

App.Colour = DS.Model.extend({
  name: DS.attr('string'),
  hex: DS.attr('string')
});

// settings the fixtures directly in the model class
App.Colour.FIXTURES = [
  {id: 1, name: 'red', hex: '#F00'}, 
  {id: 2, name: 'green', hex: '#0F0'},
  {id: 3, name: 'blue', hex: '#00F'}
];

Then in my route I query normally:

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('colour');
  } 
});

This should output the fixture records normally.



来源:https://stackoverflow.com/questions/25744233/ember-fixtures-not-working

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