Anton Shestakov <av6@dwimlabs.net>, Thu, 05 Jul 2018 22:55:53 +0800
demo: jQuery 3.3.1
test/sensible-filters.js
Permissions: -rw-r--r--
var a, b, c, d, col, otherCol; module('Backbone.Shard', { a = new Backbone.Model({data: 'a'}); b = new Backbone.Model({data: 'b'}); c = new Backbone.Model({data: 'c'}); d = new Backbone.Model({data: 'd'}); col = new Backbone.Collection([a, b, c, d]); otherCol = new Backbone.Collection(); test('Sensible filter function, collection is prepopulated', function() { var shard = new Backbone.Shard({ filter: function(model) { return _(['a', 'c']).contains(model.get('data')); } equal(shard.size(), 2, 'shard has 2 items'); equal(shard.first(), a, 'a is the first element'); equal(shard.last(), c, 'c is the last element'); test('Sensible filter function, collection populated dynamically', function() { var shard = new Backbone.Shard({ filter: function(model) { return _(['a', 'c']).contains(model.get('data')); } shard.on('add', function() { }).on('remove', function() { }).on('reset', function() { otherCol.add([a, b, c, d]); equal(additions, 2, 'adding models matching shard filter to collection triggers add events'); equal(shard.size(), 2, '... and the shard has 2 items'); equal(removals, 0, 'removing models not matching shard filter from collection does not trigger remove events'); equal(shard.size(), 2, '... and the shard has 2 items'); equal(removals, 2, 'removing models matching shard filter from collection triggers remove events'); equal(shard.size(), 0, '... and the shard is empty'); otherCol.reset([a, b, c, d]); equal(resets, 1, 'resetting collection triggers reset events'); equal(shard.size(), 2, '... and the shard has 2 items');