问题描述
我在Emberjs-Data中实现了queryFixtures函数的问题。我有以下代码定义商店:
App.Store = DS.Store.extend({
revision :12,
适配器:DS.FixtureAdapter.extend({
queryFixtures:function(fixtures,query,type){
console.log(query);
console.log类型);
返回fixtures.filter(function(item){
for(prop in query)){
if(item [prop]!= query [prop]){
返回false;
}
}
return true;
});
}
})
});
我的模型看起来像:
FireSurveyApp.User = DS.Model.extend({
userId:DS.attr('number'),
username:DS.attr('string'),
密码:DS.attr('string'),
FirstName:DS.attr('string'),
LastName:DS.attr('string')
});
当我尝试获取灯具数据时,我使用以下代码:
var returnUser = this.store.find(User,{username:Ted});
函数将返回undefined,有没有不同的方式,我应该调用queryFixtures函数? / p>
提前感谢。
我能够找出一个解。将商店适配器设置回DS.FixtureAdapter。
适配器:DS.FixtureAdapter
在ember-data js文件中实现queryFixtures函数,如图所示:
queryFixtures:function(fixtures,query,type){
var key = Ember.keys(query)[0];
返回fixtures.filterBy(key,query [key]);
},
在完成这两个步骤之后,我可以查询灯具如上所示没有问题。希望这可以帮助任何人遇到同样的问题。
I have been having an issue implementing the queryFixtures function in Emberjs-Data. I have the following code to define the Store:
App.Store = DS.Store.extend({
revision: 12,
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;
});
}
})
});
My Model looks like:
FireSurveyApp.User = DS.Model.extend({
userId: DS.attr('number'),
username: DS.attr('string'),
password: DS.attr('string'),
FirstName: DS.attr('string'),
LastName: DS.attr('string')
});
When i try to get the fixture data out i am using the following code:
var returnUser = this.store.find("User",{ username : "Ted"});
The function will return undefined, Is there a different way that i should be calling the queryFixtures function?
Thanks in advance.
I was able to figure out a solution. Set the store adapter back to DS.FixtureAdapter.
adapter: 'DS.FixtureAdapter'
Implement the queryFixtures Function in the ember-data js file as shown:
queryFixtures: function(fixtures, query, type) {
var key = Ember.keys(query)[0];
return fixtures.filterBy(key, query[key]);
},
After both these steps have been done I was able to query the fixture as shown above with no problem. Hope this helps anyone experiencing the same issue.
这篇关于Emberjs Fixture适配器queryFixture不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!