使用DS.FixtureAdapter时无法查询Emberjs模型

时间:2013-05-25 19:28:01

标签: ember.js

我无法查询我的模型。我不知道我做错了什么。

我的商店定义为

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

我的模型定义了,

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."
    }
];

当我在命令行中运行以下内容时

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

4 个答案:

答案 0 :(得分:30)

我设法通过使用David Lai的答案解决了上述错误,但没有从DS.Store(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'
});

答案 1 :(得分:21)

感谢您帮助我们解决这个问题。

从我能够收集的内容中,find()方法调用findQuery(),它调用FixturesAdapter中的queryFixtures()。没有实现的想法是开发人员扩展它以实现他们自己的find()来截取不同的搜索结果。对于参数的基本过滤器,我能够做到这一点。

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

答案 2 :(得分:0)

我猜您的问题是您缺少已定义的App前缀,以及ember-data的DS前缀。您也可以删除Feature.adapter = EmberData.FixtureAdapter.create();

我已编辑了您的代码(未经过测试)。

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."
}
];

现在要查询您应该使用的特定ID:

App.Feature.find(1);

要通过属性进行查询,您可以执行以下操作:

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

这应该有用。

希望有所帮助

答案 3 :(得分:0)

这里的问题是fixture适配器实际上并没有实现任何查询方法。如果您想通过其ID找到记录,您可以简单地调用App.Feature.find(id),但不支持任何更复杂的记录。即使使用DS.RESTAdapter,您也必须在服务器上定义自己的查询接口,以使查询正常工作。