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

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

问题描述

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

 未捕获TypeError:对象函数(){.....}没有方法'findQuery'

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

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

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

Grid.ModalModel.FIXTURES = [
{
id:1,
fname:sachin,
lname:gh,
电子邮件:gh,
contactno:4542154,
gendertype:true,
contactype:1
},
{
id: 2,
fname:amit,
lname:gh,
电子邮件:gh,
contactno:4542154,
gendertype:
contactype:2
},
{
id:3,
fname:namit,
lname:gh,
电子邮件: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。

解决方案

只需更改 findQuery 查询



此后,控制台中将显示一条错误消息:

 断言失败:未实现:您必须覆盖DS.FixtureAdapter :: queryFixtures方法才能支持查询灯具存储。 

像消息说明一样,只需实现 DS.FixtureAdapter#queryFixtures
传递给 queryFixtures 的参数是:记录,查询,类型。其中:




  • 记录是一组简单的javascript对象,您将过滤。

  • 查询是传递给您的ember数据类的查询方法的对象

  • 类型是查询所调用的ember数据类。



返回是过滤的数据。



例如,执行一个简单的例子:

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

只需重新打开您的 DS.FixtureAdapter

  DS.FixtureAdapter.reopen({
queryFixtures:function(records,query,type){
return records.filter(function(record){
for(var key在查询中){
if(!query.hasOwnProperty(key)){continue;}
var value = query [key];
if(record [key]!== value){ return false;}
}
return true;
});
}
});

这里是一个现场演示。


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

Controller Code:

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

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

解决方案

Just change findQuery to 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. 

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

  • 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.

For example, to perform a simple where like:

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

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

Here is a live demo.

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

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