David Lai
David Lai

Reputation: 822

Unable to query Emberjs Model when using DS.FixtureAdapter

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.

Upvotes: 13

Views: 5513

Answers (4)

Ivan Chaer
Ivan Chaer

Reputation: 7100

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'
});

Upvotes: 30

David Lai
David Lai

Reputation: 822

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;
            });
        }
    })
});

Upvotes: 21

rpowell
rpowell

Reputation: 1466

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.

Upvotes: 0

intuitivepixel
intuitivepixel

Reputation: 23322

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

Upvotes: 0

Related Questions