Skip to content

Commit ed96b2d

Browse files
author
Derek DeVries
committed
improve performance of quiz index search box
we were previously re-rendering the list for every model changed. Now we only render after the filtering is complete. fixes CNVS-16247 test plan: - as a teacher - create a course with 30 or more quizzes - filter the quizzes using the search box at the top of the index page - It should filter the results much faster!! Change-Id: Iafb0624a4974ff48223b87b2849c12476e098062 Reviewed-on: https://gerrit.instructure.com/43578 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Ryan Taylor <rtaylor@instructure.com> QA-Review: Trevor deHaan <tdehaan@instructure.com> Product-Review: Derek DeVries <ddevries@instructure.com>
1 parent 81d4adc commit ed96b2d

4 files changed

Lines changed: 57 additions & 48 deletions

File tree

app/coffeescripts/views/quizzes/IndexView.coffee

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,17 @@ define [
2828
@options.hasOpenQuizzes = @openView.collection.length > 0
2929
@options.hasSurveys = @surveyView.collection.length > 0
3030

31-
collections: ->
31+
views: ->
3232
[
33-
@options.assignmentView.collection
34-
@options.openView.collection
35-
@options.surveyView.collection
33+
@options.assignmentView
34+
@options.openView
35+
@options.surveyView
3636
]
3737

38-
keyUpSearch: =>
39-
clearTimeout @onInputTimer
40-
@onInputTimer = setTimeout @filterResults, 200
38+
keyUpSearch: _.debounce ->
39+
@filterResults()
40+
, 200
4141

4242
filterResults: =>
43-
term = $('#searchTerm').val()
44-
45-
_.each @collections(), (collection) =>
46-
collection.each (model) =>
47-
model.set('hidden', !@filter(model, term))
48-
49-
filter: (model, term) =>
50-
return true unless term
51-
52-
title = model.get('title').toLowerCase()
53-
numMatches = 0
54-
keys = term.toLowerCase().split(' ')
55-
for part in keys
56-
#not using match to avoid javascript string to regex oddness
57-
numMatches++ if title.indexOf(part) != -1
58-
numMatches == keys.length
43+
_.each @views(), (view) =>
44+
view.filterResults($('#searchTerm').val())

app/coffeescripts/views/quizzes/QuizItemGroupView.coffee

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,27 @@ define [
2525
isEmpty: ->
2626
@collection.isEmpty() or @collection.all((m) -> m.get('hidden'))
2727

28-
attachCollection: ->
29-
@collection.on('change:hidden', @render)
28+
filterResults: (term) =>
29+
anyChanged = false
30+
@collection.forEach (model) =>
31+
hidden = !@filter(model, term)
32+
if !!model.get('hidden') != hidden
33+
anyChanged = true
34+
model.set('hidden', hidden)
35+
36+
@render() if anyChanged
37+
38+
filter: (model, term) =>
39+
return true unless term
40+
41+
title = model.get('title').toLowerCase()
42+
numMatches = 0
43+
keys = term.toLowerCase().split(' ')
44+
for part in keys
45+
#not using match to avoid javascript string to regex oddness
46+
numMatches++ if title.indexOf(part) != -1
47+
numMatches == keys.length
48+
3049

3150
render: ->
3251
super

spec/coffeescripts/views/quizzes/IndexViewSpec.coffee

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,3 @@ define [
125125
view.filterResults()
126126
equal view.$el.find('.collectionViewItems li').length, 2
127127

128-
test 'should filter models with title that doesnt match term', ->
129-
view = indexView()
130-
model = new Quiz(title: "Foo Name")
131-
132-
ok view.filter(model, "name")
133-
ok !view.filter(model, "zzz")
134-
135-
test 'should not use regexp to filter models', ->
136-
view = indexView()
137-
model = new Quiz(title: "Foo Name")
138-
139-
ok !view.filter(model, ".*name")
140-
ok !view.filter(model, "zzz")
141-
142-
test 'should filter models with multiple terms', ->
143-
view = indexView()
144-
model = new Quiz(title: "Foo Name bar")
145-
146-
ok view.filter(model, "name bar")
147-
ok !view.filter(model, "zzz")

spec/coffeescripts/views/quizzes/QuizItemGroupViewSpec.coffee

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,37 @@ define [
3636
ok view.isEmpty()
3737

3838

39-
test 'should rerender on :hidden change', ->
39+
test 'should filter models with title that doesnt match term', ->
4040
collection = new QuizCollection([{id: 1}, {id: 2}])
4141
view = createView(collection)
42+
model = new Quiz(title: "Foo Name")
43+
44+
ok view.filter(model, "name")
45+
ok !view.filter(model, "zzz")
46+
47+
test 'should not use regexp to filter models', ->
48+
collection = new QuizCollection([{id: 1}, {id: 2}])
49+
view = createView(collection)
50+
model = new Quiz(title: "Foo Name")
51+
52+
ok !view.filter(model, ".*name")
53+
ok !view.filter(model, "zzz")
54+
55+
test 'should filter models with multiple terms', ->
56+
collection = new QuizCollection([{id: 1}, {id: 2}])
57+
view = createView(collection)
58+
model = new Quiz(title: "Foo Name bar")
59+
60+
ok view.filter(model, "name bar")
61+
ok !view.filter(model, "zzz")
62+
63+
64+
test 'should rerender on filter change', ->
65+
collection = new QuizCollection([{id: 1, title: 'hey'}, {id: 2, title: 'foo'}])
66+
view = createView(collection)
4267
equal view.$el.find('.collectionViewItems li').length, 2
4368

44-
quiz = collection.get(1)
45-
quiz.set('hidden', true)
69+
view.filterResults('hey')
4670
equal view.$el.find('.collectionViewItems li').length, 1
4771

4872
test 'should not render no content message if quizzes are available', ->

0 commit comments

Comments
 (0)