src/components/collection_view.coffee in luca-0.9.65 vs src/components/collection_view.coffee in luca-0.9.76

- old
+ new

@@ -1,6 +1,6 @@ -collectionView = Luca.define "Luca.components.CollectionView" +collectionView = Luca.register "Luca.components.CollectionView" # The CollectionView facilitates the rendering of a Collection # of models into a group of many rendered templates # # Example: # @@ -14,44 +14,50 @@ # query: # default: 'value' # collectionView.extends "Luca.components.Panel" -collectionView.behavesAs "LoadMaskable", +collectionView.mixesIn "QueryCollectionBindings", + "LoadMaskable", "Filterable", "Paginatable" collectionView.triggers "before:refresh", "after:refresh", "refresh", "empty:results" -collectionView.defaults - +# IDEA: +# +# For validation of component configuration, +# we could define a convention like: +# +# collectionView.validatesConfigurationWith +# requiresValidCollectionAt: "collection" +# requiresPresenceOf: +# either: ["itemTemplate", "itemRenderer", "itemProperty"] +# +# +collectionView.publicConfiguration tagName: "ol" - - className: "luca-ui-collection-view" - bodyClassName: "collection-ui-panel" - - # A collection view can pass a model through to a template + itemTagName: 'li' + itemClassName: 'collection-item' itemTemplate: undefined - - # A collection view can pass a model through a function which should return a string itemRenderer: undefined + itemProperty: undefined - itemTagName: 'li' - - itemClassName: 'collection-item' - - +collectionView.defines initialize: (@options={})-> _.extend(@, @options) - _.bindAll @, "refresh" - + # IDEA: + # + # This type of code could be moved into a re-usable concern + # which higher order components can mixin to make it easier + # to extend them, instantiate them, etc. unless @collection? or @options.collection console.log "Error on initialize of collection view", @ throw "Collection Views must specify a collection" unless @itemTemplate? || @itemRenderer? || @itemProperty? @@ -65,33 +71,28 @@ unless Luca.isBackboneCollection(@collection) console.log "Missing Collection on #{ @name || @cid }", @, @collection throw "Collection Views must have a valid backbone collection" - @collection.on "before:fetch", ()=> - @trigger "enable:loadmask" - - @collection.bind "reset", ()=> - @refresh() - @trigger "disable:loadmask" + @collection.on "before:fetch", ()=> + @trigger "enable:loadmask" + + @collection.bind "reset", ()=> + @refresh() + @trigger "disable:loadmask" - @collection.bind "remove", ()=> - @refresh() + @collection.bind "remove", ()=> + @refresh() - @collection.bind "add", ()=> - @refresh() + @collection.bind "add", ()=> + @refresh() - if @observeChanges is true - @collection.on "change", @refreshModel, @ + if @observeChanges is true + @collection.on "change", @refreshModel, @ Luca.components.Panel::initialize.apply(@, arguments) - unless @autoRefreshOnModelsPresent is false - @defer ()=> - @refresh() if @collection.length > 0 - .until("after:render") - @on "refresh", @refresh, @ attributesForItem: (item, model)-> _.extend {}, class: @itemClassName, "data-index": item.index, "data-model-id": item.model.get('id') @@ -116,47 +117,9 @@ try make(@itemTagName, attributes, content) catch e console.log "Error generating DOM element for CollectionView", @, model, index #no op - - getCollection: ()-> - @collection - - applyQuery: (query={},queryOptions={})-> - @query = query - @queryOptions = queryOptions - @refresh() - @ - - # Private: returns the query that is applied to the underlying collection. - # accepts the same options as Luca.Collection.query's initial query option. - getQuery: ()-> - query = @query ||= {} - for querySource in _( @querySources || [] ).compact() - query = _.extend(query, (querySource()||{}) ) - query - - # Private: returns the query that is applied to the underlying collection. - # accepts the same options as Luca.Collection.query's initial query option. - getQueryOptions: ()-> - options = @queryOptions ||= {} - - for optionSource in _( @optionsSources || [] ).compact() - options = _.extend(options, (optionSource()||{}) ) - - options - - # Private: returns the models to be rendered. If the underlying collection - # responds to @query() then it will use that interface. - getModels: (query,options)-> - if @collection?.query - query ||= @getQuery() - options ||= @getQueryOptions() - - @collection.query(query, options) - else - @collection.models locateItemElement: (id)-> @$(".#{ @itemClassName }[data-model-id='#{ id }']") refreshModel: (model)->