Backbone.js does not provide a view or controller that specifically manages a collection. Backbone view objects are coupled with a model and have render methods for presented the model data using an HTML template. However there are many instanced where a Backbone collection objects (has many models) needs to be presented or managed with a single view object. Thus the need for a CollectionView constructor that implements an interface to manage many child views which render each model represented in the collection. A collection view object that generates view for each model preserves the core Backbone behavior (link is to documented source, search for ‘change’) of firing change events on a model and a view that renders the changed data in response to the change event that was triggered by specific models.
Liquid Media has posted tutorials on Backbone in a 3-part series 1, 2, 3; and the third article demonstrates the use of a Collection View constructor which does what is described above.
CollectionView
This constructor is based on the article by Liquid Media
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
Header view with factory method to generate multiple collection views
This view has a custom method buildCollectionView with is a factory for generating multiple collection view objects based on a multi-dimensional array containing segments of events by type and schedule. See the block that contains…
1 2 3 4 5 6 7 | |
The CollectionView constructor requires a Backbone collection instance and a Backbone Model constructor as well as properties tagname and el which are used when initializing instances of the view constructor for each model in the collection instance.
Full implementation example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
Examples when Collection Views are used as a product’s variants for color and size
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
The color collection and view used by the Information view which acts as a view manager
Colors Collection
1 2 3 4 5 6 7 8 9 | |
Colors View
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |