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 thefilter
attribute on its state model, called_filterState
.If the value has changed, the
filtered
attribute 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 apagerEl
selector, which displays pagination controls.It initializes with a passable
length
option, which determines how many whole items will be shown at once in theviewportEl
; it defaults to10
. This sets thelength
attribute on the_paginationState
model, 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-y
CSS rule set toscroll
orauto
.It initializes with a passable
length
option, which determines how many whole items will be shown at once in theviewportEl
; it defaults to10
. This sets thelength
attribute on the_scrollState
model, which tracks internal state.When the list is initialized, if
spacerEl
does not exist, thenspacerTemplate
is evaluated and made the first child of theviewportEl
. Likewise, iflistEl
does not exist,listTemplate
is evaluated and made the last child ofviewportEl
.Then,
getSize()
evaluatedlistTemplate
with a single dummy model inside a dummy collection, which calculates theclientHeight
of an item underneathlistEl
.When the jQuery
scroll()
event is triggered onviewportEl
, the innerspacerEl
is adjusted to offset the top of the list. Then, the height of thelistEl
is adjusted to reflect the new offset.When the
filter
event 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.