@@ -37,3 +37,29 @@ test("manages its index correctly", function() {
37
37
equal ( index . get ( 2 ) . length , 0 , "model is removed from the index" )
38
38
equal ( story2 . _events [ "change:project_id" ] . length , 0 , "Indexer callbacks are cleaned up" )
39
39
} )
40
+
41
+ test ( "with a custom #toKey" , function ( ) {
42
+ var Story = Model ( "story" )
43
+
44
+ var story1 = new Story ( { when : new Date ( 2012 , 2 , 3 ) } )
45
+ var story2 = new Story ( { when : new Date ( 2012 , 2 , 14 ) } )
46
+ var story3 = new Story ( { when : new Date ( 2012 , 2 , 3 ) } )
47
+
48
+ var index = new Model . Indexer ( Story . collection , "when" )
49
+
50
+ index . toKey = function ( model ) {
51
+ var when = model . get ( "when" )
52
+ return [ when . getFullYear ( ) , when . getMonth ( ) , when . getDate ( ) ] . join ( "-" )
53
+ }
54
+
55
+ Story . collection . add ( story1 )
56
+ Story . collection . add ( story2 )
57
+ Story . collection . add ( story3 )
58
+
59
+ equal ( index . get ( "2012-2-3" ) . length , 2 )
60
+ equal ( index . get ( "2012-2-14" ) . length , 1 )
61
+
62
+ ok ( index . get ( "2012-2-3" ) . first ( ) === story1 )
63
+ ok ( index . get ( "2012-2-3" ) . last ( ) === story3 )
64
+ ok ( index . get ( "2012-2-14" ) . first ( ) === story2 )
65
+ } )
0 commit comments