This assortment of views for Backbone.js demonstrates complex View and Collection logic, dependent upon Underwear.js and a bit of cleverness.
These examples are built for LOC brevity and simplicity, not speed, but their production versions work quite fine.
The original jQuery version of this code can be found here at EMI's github page and github repo.
The view zoo contains 5 examples of views, including mutations:
-
This view accepts
filterEl, a selector for a text input element. The selected input element is monitored for keyup events and sets thefilterattribute on its state model, called_filterState.If the value has changed, the
filteredattribute is updated by selecting matching models from the view'scollection. The current selection mechanism provides anO(n)search time. Faster search mechanisms are advisable. -
This view mutates the Filterable view to add a pagination manager, which can be appropriate for use cases where scrolling is not appropriate. The view accepts a
viewportEl, which displays the output and apagerElselector, which displays pagination controls.It initializes with a passable
lengthoption, which determines how many whole items will be shown at once in theviewportEl; it defaults to10. This sets thelengthattribute on the_paginationStatemodel, which tracks internal state. -
This view mutates the Filterable View to add a scroll manager, a critical component for displaying thousand-item lists. The view accepts
viewportEl, a selector for a container with theoverflow-yCSS rule set toscrollorauto.It initializes with a passable
lengthoption, which determines how many whole items will be shown at once in theviewportEl; it defaults to10. This sets thelengthattribute on the_scrollStatemodel, which tracks internal state.When the list is initialized, if
spacerEldoes not exist, thenspacerTemplateis evaluated and made the first child of theviewportEl. Likewise, iflistEldoes not exist,listTemplateis evaluated and made the last child ofviewportEl.Then,
getSize()evaluatedlistTemplatewith a single dummy model inside a dummy collection, which calculates theclientHeightof an item underneathlistEl.When the jQuery
scroll()event is triggered onviewportEl, the innerspacerElis adjusted to offset the top of the list. Then, the height of thelistElis adjusted to reflect the new offset.When the
filterevent fires, the list is scrolled back to the top.
Filtered List uses Jasmine tests and assumes a Ruby environment.
- gem install bundler
- bundle install
- rake jasmine
and now you will be able to run the tests at http://localhost:8888
Copyright (c) 2011 Doc Ritezel
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.