[英]Using filter inside Ember.RSVP.hash fails to retrieve data in qunit test
我在使用两个模型并使用Ember.RSVP.hash
的路由上进行了qunit测试。 事情按预期工作,但是一旦我向Ember.RSVP.hash
添加filter
,它就会停止检索qunit中的数据。 在qunit之外,它仍然可以工作,因此我认为这是filter
, Ember.RSVP.hash
和qunit
。
这是我的测试:
module('Ember.js Library', {
setup: function() {
Ember.run(App, App.advanceReadiness);
},
teardown: function() {
App.reset();
}
});
test('foo', function() {
visit('/books');
andThen(function() {
equal(currentRouteName(), 'books.index');
});
});
这是不使用filter
的工作路线:
App.BooksRoute = Ember.Route.extend({
model: function() {
var id = 1;
// note this does not filter "books" properly
return Ember.RSVP.hash({
books: this.store.find('books'),
library: this.store.find('library', id)
});
}
});
这是使用filter
的版本,无法使用:
App.BooksRoute = Ember.Route.extend({
model: function() {
var id = 1;
// note: filters "books" properly but doesn't work in qunit
return Ember.RSVP.hash({
books: this.store.filter('books', function(record) {
return record.get('libraryId') === id;
}),
library: this.store.find('library', id)
});
}
});
最终结果是,在工作版本中, books
按预期方式存在。 在第二版中, books
最终是空的。 在这两个版本中, library
始终存在,因此我相信filter
是问题所在。
我如何使用filter
并使qunit正常工作?
store.filter
实际上不会加载记录,它只会返回商店中已经存在的记录。 您可能想做的是...
App.BooksRoute = Ember.Route.extend({
model: function() {
var id = 1;
// note: filters "books" properly but doesn't work in qunit
return Ember.RSVP.hash({
books: this.store.find('books').then(function(books){
return books.filterBy('libraryId', id)
}),
library: this.store.find('library', id)
});
}
});
(免责声明:上面的代码未经测试,但这就是想法)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.