Пример #1
0
test('In memory', function() {
  var collection = new PostCollection(null, {processInMemory: true});

  this.ajaxResponse = {results: ExampleData.POSTS};
  var eventLog = new EventLog(collection);
  collection.load();
  equal(collection.size(), 3);
  equal(this.ajaxCount, 1, 'should load both parent and remote children');
  equal(eventLog.counts.load, 1, 'should fire load on inital load');


  collection.load();
  equal(this.ajaxCount, 1, 'should not load again if processInMemory');
  equal(eventLog.counts.load, 2, 'should fire load on in memory load');

  equal(collection.size(), 3);

  collection.set({query: '2'});
  collection.load();
  equal(collection.size(), 1, 'should query in memory');
  equal(collection.at(0).get('id'), 2);

  collection.set({
    query: undefined,
    filter: {id: 1}
  });
  collection.addFilter(new Filter({key: 'id', value: 1, comparison: 'equals'}));
  collection.load();
  equal(collection.size(), 1, 'should filter in memory');
  equal(collection.at(0).get('id'), 1);
});
Пример #2
0
test('Filters', function() {
  var collection = new PostCollection(ExampleData.POSTS, {processInMemory: true});
  collection.onLoad();
  ok(collection.getRelated('filters') !== undefined, 'should have filters');

  collection.addFilter(
    new Filter({key: 'body', value: 'my body2', comparison: 'equals'}
  ));

  collection.updateInMemory();
  equal(collection.size(), 1, 'should filter all but one');
  collection.getFilters('body')[0].set({value: 'my body', comparison: 'contains'});
  collection.updateInMemory();
  equal(collection.size(), 3, 'all items should pass contains filter');

  collection.addFilter(new Filter({key: 'body', value: '2', comparison: 'contains'}));
  collection.updateInMemory();
  equal(collection.size(), 1, 'should apply multiple filters');

  equal(JSON.parse(atob(collection.getQueryParams().filters))[0].value, 'my body', 'should include filters in query params');
});