findQuery() 在 ember-data 中不起作用? [英] findQuery() is not working in ember-data?

查看:15
本文介绍了findQuery() 在 ember-data 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Fixture contain list of contacts and each contact has contact type. I am trying to filtrate contact records using .findQuery() but it is throwing following error :

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

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

I have listed my code here :

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.

I have changed .findQuery to .query but every time it is showing length as 0.

推荐答案

只需将findQuery改为query即可.

此后,控制台中将出现错误消息:

After this a error message will appear in console:

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

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

Like the message explain, just implement the DS.FixtureAdapter#queryFixtures. The parameters passed to queryFixtures is: records, query, type. Where:

  • Records 是一个纯 javascript 对象的数组,您将对其进行过滤.
  • Query 是传递给 ember 数据类的 query 方法的对象.
  • Type 是 ember 数据类,其中查询是调用.
  • Records is an array of plain javascript objects, that you will filter.
  • Query is the object passed to query method of your ember data class.
  • Type is the ember data class, where the query is call.

返回的是过滤后的数据.

And the return is the filtered data.

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

For example, to perform a simple where like:

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

只需使用以下命令重新打开您的 DS.FixtureAdapter:

Just reopen your DS.FixtureAdapter with:

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

这里是现场演示.

这篇关于findQuery() 在 ember-data 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆