Anton Shestakov <engored@ya.ru>, Wed, 12 Dec 2012 19:09:41 +0900
Demo page (currently demonstrating event propagation issues in shard).
test/underscore-methods.js
Permissions: -rw-r--r--
var a, b, c, d, col, shard; 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]); shard = new Backbone.Shard({ filter: function(model) { return model.get('data') != 'd'; } test('Underscore methods: basics', function() { equal(shard.first(), a, 'first() works'); equal(shard.last(), c, 'last() works'); deepEqual(shard.initial(), [a, b], 'initial() works'); deepEqual(shard.rest(), [b, c], 'rest() works'); equal(shard.indexOf(c), 2, 'indexOf() works'); equal(shard.lastIndexOf(c), 2, 'lastIndexOf() works'); equal(shard.isEmpty(), false, 'isEmpty() works'); deepEqual(shard.toArray(), [a, b, c], 'toArray() works'); equal(shard.contains(b), true, 'contains() works'); deepEqual(shard.without(b), [a, c], 'without() works'); equal(shard.all(_.identity), true, 'all() works'); equal(shard.any(), true, 'any() works'); test('Underscore methods: searching and filtering', function() { equal(shard.min(function(model) { return model.get('data').charCodeAt(0); equal(shard.max(function(model) { return model.get('data').charCodeAt(0); equal(shard.find(function(model) { return model.get('data') == 'b'; deepEqual(shard.filter(function(model) { return model.get('data') != 'b'; }), [a, c], 'filter() works'); deepEqual(shard.reject(function(model) { return model.get('data') == 'b'; }), [a, c], 'reject() works'); test('Underscore methods: iterating over models', function() { shard.each(function(model) { sdata += model.get('data'); }); equal(sdata, 'abc', 'each() works'); equal(shard.map(function(model) { return model.get('data'); }).join(' '), 'a b c', 'map() works'); shard.invoke('set', 'updated', true); equal(_.all(shard.pluck('updated')), true, 'invoke() works'); test('Underscore methods: data hunt', function() { equal(shard.pluck('data').join(' '), 'a b c', 'pluck() works'); .map(function(model) { return model.get('data'); }) .value(), 'a', 'chain() works'); equal(shard.reduce(function(memo, model) { return memo += model.get('data'); }, ''), 'abc', 'reduce() works'); equal(shard.reduceRight(function(memo, model) { return memo += model.get('data'); }, ''), 'cba', 'reduceRight() works');