简体   繁体   English

在Ember.RSVP.hash中使用过滤器无法在qunit测试中检索数据

[英]Using filter inside Ember.RSVP.hash fails to retrieve data in qunit test

I have a qunit test on a route that uses TWO models and use Ember.RSVP.hash to accomplish this. 我在使用两个模型并使用Ember.RSVP.hash的路由上进行了qunit测试。 Things work as expected but as soon as I add a filter to Ember.RSVP.hash it stops retrieving data in qunit. 事情按预期工作,但是一旦我向Ember.RSVP.hash添加filter ,它就会停止检索qunit中的数据。 Outside of qunit it still works so I believe it's an issue with filter , Ember.RSVP.hash and qunit . 在qunit之外,它仍然可以工作,因此我认为这是filterEmber.RSVP.hashqunit

Here's my test: 这是我的测试:

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

Here's the working route that does not use filter : 这是不使用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)
    });
  }
});

Here's the version that uses filter and does not work: 这是使用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)
    });
  }
});

The end result is that in the working version, books is present as expected. 最终结果是,在工作版本中, books按预期方式存在。 In the second version, books ends up being empty. 在第二版中, books最终是空的。 In both versions the library is always present so I believe filter is the problem. 在这两个版本中, library始终存在,因此我相信filter是问题所在。

How can I use filter and get things to work with qunit? 我如何使用filter并使qunit正常工作?

store.filter doesn't actually load the records, it will only return records that are already in the store. store.filter实际上不会加载记录,它只会返回商店中已经存在的记录。 What you probably want to do is something like... 您可能想做的是...

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

(disclaimer: the code above is untested but thats the idea) (免责声明:上面的代码未经测试,但这就是想法)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM