Anton Shestakov <engored@ya.ru>, Fri, 14 Dec 2012 23:54:33 +0900
Changes for Backbone 0.9.9: Shards now also proxy Underscore method name aliases.
test/basic-filters.js
Permissions: -rw-r--r--
var a, b, c, col, otherCol, additions, removals, resets; module('Backbone.Shard', { a = new Backbone.Model({data: 'a'}); b = new Backbone.Model({data: 'b'}); c = new Backbone.Model({data: 'c'}); col = new Backbone.Collection([a, b, c]); otherCol = new Backbone.Collection(); test('All-exclusive filter function, collection is prepopulated', function() { var shard = new Backbone.Shard({ filter: function() { return false; } equal(shard.size(), 0, 'shard is empty'); equal(shard.first(), undefined, 'nothing is the first element'); equal(shard.last(), undefined, 'nothing is the last element'); test('All-exclusive filter function, collection populated dynamically', function() { var shard = new Backbone.Shard({ filter: function() { return false; } shard.on('add', function() { }).on('remove', function() { }).on('reset', function() { equal(additions, 0, 'adding models not matching shard filter to collection does not trigger add events'); equal(shard.size(), 0, '... and the shard is empty'); otherCol.remove([a, b, c]); equal(removals, 0, 'removing models not matching shard filter from collection does not trigger remove events'); equal(shard.size(), 0, '... and the shard is empty'); otherCol.reset([a, b, c]); equal(resets, 1, 'resetting collection triggers reset events'); equal(shard.size(), 0, '... and the shard is empty'); test('All-inclusive filter function, collection is prepopulated', function() { var shard = new Backbone.Shard({ filter: function() { return true; } equal(shard.size(), 3, 'shard has 3 items'); equal(shard.first(), a, 'a is the first element'); equal(shard.indexOf(b), 1, 'b is the second element'); equal(shard.last(), c, 'c is the last element'); test('All-inclusive filter function, collection populated dynamically', function() { var shard = new Backbone.Shard({ filter: function() { return true; } shard.on('add', function() { }).on('remove', function() { }).on('reset', function() { equal(additions, 3, 'adding models matching shard filter to collection triggers add events'); equal(shard.size(), 3, '... and the shard has 3 items'); otherCol.remove([a, b, c]); equal(removals, 3, 'removing models matching shard filter from collection triggers remove events'); equal(shard.size(), 0, '... and the shard is empty'); otherCol.reset([a, b, c]); equal(resets, 1, 'resetting collection triggers reset events'); equal(shard.size(), 3, '... and the shard has 3 items');