13

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

我的商店定义为

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

4 回答 4

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'
});
于 2013-11-04T07:05:18.823 回答
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;
            });
        }
    })
});
于 2013-05-28T22:22:56.927 回答
0

我猜您的问题是您缺少App已经定义的DS前缀,以及 ember-data 的前缀。您也可以删除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" });

这应该有效。

希望能帮助到你

于 2013-05-25T20:14:04.680 回答
0

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

于 2013-05-26T00:56:21.537 回答