CollectionView and SearchView updates
- fixed issue where items weren't being inserted because they couldn't find the sibling element in CollectionView - typos in View.coffee - abort old requests in SearchView - doc formatting changes Change-Id: I0851d994338c8b5050865354b4e4bf4c3ffb1a45 Reviewed-on: https://gerrit.instructure.com/17618 Reviewed-by: Jon Jensen <jon@instructure.com> Tested-by: Jenkins <jenkins@instructure.com> QA-Review: Ryan Florence <ryanf@instructure.com>
This commit is contained in:
parent
fe4cbf8ee9
commit
a70a6d91fc
|
@ -166,8 +166,8 @@ define [
|
|||
#
|
||||
# 1. `@model.present()`
|
||||
# 2. `@model.toJSON()`
|
||||
# 3. `@colleciton.present()`
|
||||
# 4. `@colleciton.toJSON()`
|
||||
# 3. `@collection.present()`
|
||||
# 4. `@collection.toJSON()`
|
||||
# 5. `@options`
|
||||
#
|
||||
# Using `present` is encouraged so that when a model or collection is saved
|
||||
|
@ -240,7 +240,7 @@ define [
|
|||
# class RecentItemsView
|
||||
# events:
|
||||
# 'click .header': 'expand'
|
||||
# 'click .something a': 'stopPropagation'
|
||||
# 'click .header a': 'stopPropagation'
|
||||
#
|
||||
# @param {$Event} event
|
||||
# @api public
|
||||
|
|
|
@ -13,14 +13,15 @@ define [
|
|||
# view = new CollectionView
|
||||
# itemView: PersonView
|
||||
# collection: peopleCollection
|
||||
# peopleCollection.add(someThing)
|
||||
# peopleCollection.add name: 'ryanf', id: 1
|
||||
# peopleCollection.fetch()
|
||||
# # etc.
|
||||
#
|
||||
|
||||
class CollectionView extends Backbone.View
|
||||
|
||||
##
|
||||
# The backbone view rendered for collection items
|
||||
|
||||
@optionProperty 'itemView'
|
||||
|
||||
className: 'collectionView'
|
||||
|
@ -31,17 +32,27 @@ define [
|
|||
##
|
||||
# When using a different template ensure it contains an element with a
|
||||
# class of `.collectionViewItems`
|
||||
|
||||
template: template
|
||||
|
||||
##
|
||||
# Options:
|
||||
#
|
||||
# - `itemView` {Backbone.View}
|
||||
# - `collection` {Backbone.Collection}
|
||||
#
|
||||
# @param {Object} options
|
||||
# @api public
|
||||
initialize: ->
|
||||
|
||||
initialize: (options) ->
|
||||
super
|
||||
@attachCollection()
|
||||
|
||||
##
|
||||
# Renders the main template and the item templates
|
||||
#
|
||||
# @api public
|
||||
|
||||
render: =>
|
||||
super
|
||||
@renderItems() if @collection.length
|
||||
|
@ -52,10 +63,11 @@ define [
|
|||
|
||||
##
|
||||
# Attaches all the collection events
|
||||
#
|
||||
# @api private
|
||||
|
||||
attachCollection: ->
|
||||
@collection.on 'reset', @removePreviousItems
|
||||
@collection.on 'reset', @render
|
||||
@collection.on 'reset', @renderOnReset
|
||||
@collection.on 'add', @renderOnAdd
|
||||
@collection.on 'remove', @removeItem
|
||||
@collection.on 'remove', @rerenderUnlessCollection
|
||||
|
@ -63,26 +75,41 @@ define [
|
|||
##
|
||||
# Ensures item views are removed properly, when we upgrade backbone we can
|
||||
# use options.previousModels instead of the DOM.
|
||||
#
|
||||
# @param {Array} models - array of Backbone.Models
|
||||
# @api private
|
||||
|
||||
removePreviousItems: (models) =>
|
||||
@$list.children().each (index, el) =>
|
||||
@$(el).data('view').remove()
|
||||
|
||||
renderOnReset: =>
|
||||
@removePreviousItems()
|
||||
@render()
|
||||
|
||||
##
|
||||
# Renders all collection items
|
||||
#
|
||||
# @api private
|
||||
|
||||
renderItems: ->
|
||||
@collection.each @renderItem
|
||||
|
||||
##
|
||||
# Removes an item
|
||||
#
|
||||
# @param {Backbone.Model} model
|
||||
# @api private
|
||||
|
||||
removeItem: (model) =>
|
||||
model.view.remove()
|
||||
|
||||
##
|
||||
# Ensures main template is rerendered when the first item is added
|
||||
#
|
||||
# @param {Backbone.Model} model
|
||||
# @api private
|
||||
|
||||
renderOnAdd: (model) =>
|
||||
if @collection.length is 1
|
||||
@render()
|
||||
|
@ -91,13 +118,18 @@ define [
|
|||
|
||||
##
|
||||
# Ensures the template rerenders when there is no collection
|
||||
#
|
||||
# @api private
|
||||
|
||||
rerenderUnlessCollection: =>
|
||||
@render() unless @collection.length
|
||||
|
||||
##
|
||||
# Renders an item with the `itemView`
|
||||
#
|
||||
# @param {Backbone.Model} model
|
||||
# @api private
|
||||
|
||||
renderItem: (model) =>
|
||||
view = new @itemView {model}
|
||||
view.render()
|
||||
|
@ -105,13 +137,28 @@ define [
|
|||
|
||||
##
|
||||
# Inserts the item view with respect to the collection comparator.
|
||||
#
|
||||
# @param {Backbone.View} view
|
||||
# @api private
|
||||
|
||||
insertView: (view) ->
|
||||
index = @collection.indexOf view.model
|
||||
if index is 0
|
||||
@$list.prepend view.el
|
||||
@prependView view
|
||||
else if index is @collection.length - 1
|
||||
@$list.append view.el
|
||||
@appendView view
|
||||
else
|
||||
@$list.children().eq(index).before view.el
|
||||
@insertViewAtIndex view, index
|
||||
|
||||
insertViewAtIndex: (view, index) ->
|
||||
$sibling = @$list.children().eq(index)
|
||||
if $sibling.length
|
||||
$sibling.before view.el
|
||||
else
|
||||
@$list.append view.el
|
||||
|
||||
prependView: (view) ->
|
||||
@$list.prepend view.el
|
||||
|
||||
appendView: (view) ->
|
||||
@$list.append view.el
|
||||
|
|
|
@ -23,6 +23,7 @@ define ['Backbone', 'jst/searchView'], (Backbone, template) ->
|
|||
@inputFilterView.on 'enter', @fetchResults
|
||||
|
||||
fetchResults: (query) =>
|
||||
@lastRequest?.abort()
|
||||
@collection.setParam 'search_term', query
|
||||
@collection.fetch()
|
||||
@lastRequest = @collection.fetch()
|
||||
|
||||
|
|
Loading…
Reference in New Issue