4

夹具包含联系人列表,每个联系人都有联系人类型。我正在尝试使用 .findQuery() 过滤联系人记录,但它会引发以下错误:

Uncaught TypeError: Object function () {.....} has no method 'findQuery' 

我在这里列出了我的代码:

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

    Grid.ModalModel =  DS.Model.extend({
    fname: DS.attr('string'),
    lname: DS.attr('string'),
    email: DS.attr('string'),
    contactno: DS.attr('string'),
    gendertype: DS.attr('boolean'),
    contactype: DS.attr('number')
});

Grid.ModalModel.FIXTURES = [
                       {
                         id: 1,
                         fname: "sachin",
                         lname: "gh",
                         email: "gh",
                         contactno: "4542154",
                         gendertype: true,
                         contactype: 1
                       },
                       {
                         id: 2,
                         fname: "amit",
                         lname: "gh",
                         email: "gh",
                         contactno: "4542154",
                         gendertype: true,
                         contactype: 2
                       },
                       {
                         id: 3,
                         fname: "namit",
                         lname: "gh",
                         email: "gh",
                         contactno: "4542154",
                         gendertype: true,
                         contactype: 1
                       }
                      ];

控制器代码:

        totpersonalcontact:function(){ 
        return Grid.ModalModel.findQuery({ contactype: 2 }).get('length'); 
    }.property('@each.isLoaded'),
    totfriendcontact:function(){ 
        return Grid.ModalModel.findQuery({ contactype: 3 }).get('length'); 
    }.property('@each.isLoaded')

我已将 .findQuery 更改为 .query 但每次显示长度为 0。

4

1 回答 1

17

只需更改findQueryquery.

在此之后,控制台中将出现一条错误消息:

Assertion failed: Not implemented: You must override the DS.FixtureAdapter::queryFixtures method to support querying the fixture store. 

就像消息解释一样,只需实现DS.FixtureAdapter#queryFixtures. 传递给的参数queryFixtures是:记录、查询、类型。在哪里:

  • Records是一个纯 javascript 对象数组,您将对其进行过滤。
  • Query是传递给query您的 ember 数据类的方法的对象。
  • Type是 ember 数据类,其中调用了查询。

返回的是过滤后的数据。

例如,执行一个简单的 where 像:

App.Person.query({ firstName: 'Tom' })

只需重新打开您DS.FixtureAdapter的:

DS.FixtureAdapter.reopen({
    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;
        });
    }
});

是一个现场演示。

于 2013-08-10T20:07:52.670 回答