Unable to query Emberjs Model when using DS.FixtureAdapter

只愿长相守 提交于 2019-12-21 03:38:19

问题


I'm unable to query my models. I don't know what I'm doing wrong.

I have my store defined as

App.Store = DS.Store.extend({
        revision: 12,
        adapter: DS.FixtureAdapter
    });

And my model defined,

var Feature = DS.Model.extend({
    name: DS.attr('string'),
    description: DS.attr('string'),
    parent: DS.belongsTo('SimpleTestManager.Feature'),
    DS.belongsTo('SimpleTestManager.Project'),
    children: DS.hasMany('SimpleTestManager.Feature'),
    requirements: DS.attr('string')
});

App.Feature.adapter = DS.FixtureAdapter.create();

App.Feature.FIXTURES = [
    {
        id: 1,
        name: "my first feature",
        description: "some description",
        parent: null,
        project: 1,
        children:[2],
        requirements: "This is my first feature.  It has many requirements."
    },
    {
        id: 2,
        name: "a sub feature",
        description: "some sub feature.",
        parent: 1,
        project: 1,
        children:[],
        requirements: "This is a sub feature."
    }
];

When I run the following in the command line

>>App.Features.find({id:'1'})
Error: assertion failed: Not implemented: You must override the DS.FixtureAdapter::queryFixtures method to support querying the fixture store.

回答1:


I managed to solve the mentioned error by using David Lai's answer, but without extending from DS.Store (the new way after Ember Data 1.0.beta.1):

App.FixtureAdapter = DS.FixtureAdapter.extend({
  queryFixtures: function(records, query, type) {
    return records.filter(function(record) {
        for(var key in query) {
            if (!query.hasOwnProperty(key)) { continue; }
            var value = query[key];
            if (record[key] !== value) { return false; }
        }
        return true;
    });
  }
});


App.Store = DS.Store.extend({
  adapter: 'Fixture'
});



回答2:


Thanks for your help in trying to figure this out.

From what I've been able to gather, the find() method calls findQuery(), which calls queryFixtures() in the FixturesAdapter. The idea for this not being implemented is for developers to extend this to implement their own find() for stubbing out different search results. For a basic filter on parameter, I was able to just do this.

////// Stub data store fixture adapter ///////
App.Store = DS.Store.extend({
    revision: 12,
    //adapter: 'DS.RESTAdapter',
    adapter: DS.FixtureAdapter.extend({
        queryFixtures: function(fixtures, query, type) {
            console.log(query);
            console.log(type);
            return fixtures.filter(function(item) {
                for(prop in query) {
                    if( item[prop] != query[prop]) {
                        return false;
                    }
                }
                return true;
            });
        }
    })
});



回答3:


I guess your problem is that your are missing the App prefix you have already defined, and also the DS prefix for ember-data. You can also remove Feature.adapter = EmberData.FixtureAdapter.create();.

I've edited your code (not tested).

App.Store = DS.Store.extend({
    revision: 12,
    adapter: 'DS.FixtureAdapter'
});

App.Feature = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  parent: DS.belongsTo('SimpleTestManager.Feature'),
  project:DS.belongsTo('SimpleTestManager.Project'),
  children: DS.hasMany('SimpleTestManager.Feature'),
  requirements: DS.attr('string')
});

App.Feature.FIXTURES = [
{
    id: 1,
    name: "my first feature",
    description: "some description",
    parent: null,
    project: 1,
    children:[2],
    requirements: "This is my first feature.  It has many requirements."
},
{
    id: 2,
    name: "a sub feature",
    description: "some sub feature.",
    parent: 1,
    project: 1,
    children:[],
    requirements: "This is a sub feature."
}
];

Now to query a specific id you should use:

App.Feature.find(1);

To query by a property you can do:

App.Feature.findQuery({ name: "a sub feature" });

This should work.

Hope it helps




回答4:


The issue here is that the fixture adapter doesn't actually implement any query methods. If you want to find a record by its ID you can simply call App.Feature.find(id) but anything more complicated than that isn't supported. Even with the DS.RESTAdapter you have to define your own query interface on the server to make queries work properly.



来源:https://stackoverflow.com/questions/16753150/unable-to-query-emberjs-model-when-using-ds-fixtureadapter

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