src/components/collection_view.coffee in luca-0.9.6 vs src/components/collection_view.coffee in luca-0.9.7

- 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,21 +14,20 @@ # query: # default: 'value' # collectionView.extends "Luca.components.Panel" -collectionView.behavesAs "LoadMaskable", - "Filterable", - "Paginatable" +collectionView.mixesIn "LoadMaskable", + "Filterable", + "Paginatable" -collectionView.triggers "before:refresh", - "after:refresh", - "refresh", - "empty:results" +collectionView.triggers "before:refresh", + "after:refresh", + "refresh", + "empty:results" -collectionView.defaults - +collectionView.defines tagName: "ol" className: "luca-ui-collection-view" bodyClassName: "collection-ui-panel" @@ -41,28 +40,32 @@ itemTagName: 'li' itemClassName: 'collection-item' + initialize: (@options={})-> _.extend(@, @options) _.bindAll @, "refresh" + unless @collection? or @options.collection console.log "Error on initialize of collection view", @ throw "Collection Views must specify a collection" unless @itemTemplate? || @itemRenderer? || @itemProperty? throw "Collection Views must specify an item template or item renderer function" - Luca.components.Panel::initialize.apply(@, arguments) + if _.isString(@collection) + if Luca.CollectionManager.get() + @collection = Luca.CollectionManager.get().getOrCreate(@collection) + else + console.log "String Collection but no collection manager" - if _.isString(@collection) and Luca.CollectionManager.get() - @collection = Luca.CollectionManager.get().getOrCreate(@collection) - 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" @@ -77,16 +80,18 @@ @refresh() 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 "collection:change", @refresh, @ + @on "refresh", @refresh, @ attributesForItem: (item, model)-> _.extend {}, class: @itemClassName, "data-index": item.index, "data-model-id": item.model.get('id') contentForItem: (item={})-> @@ -114,20 +119,37 @@ #no op getCollection: ()-> @collection + loadModels: (models=[], options={})-> + @getCollection()?.reset(models, options) + + 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 = @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: ()-> - @queryOptions ||= {} + 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() @@ -143,23 +165,22 @@ refreshModel: (model)-> index = @collection.indexOf( model ) @locateItemElement(model.get('id')).empty().append( @contentForItem({model,index}, model) ) @trigger("model:refreshed", index, model) - refresh: (query,options)-> - query ||= @getQuery() + refresh: (query,options,models)-> + query ||= @getQuery() options ||= @getQueryOptions() + models ||= @getModels(query, options) @$bodyEl().empty() - models = @getModels(query, options) @trigger("before:refresh", models, query, options) if models.length is 0 @trigger("empty:results") index = 0 - for model in models @$append @makeItem(model, index++) @trigger("after:refresh", models, query, options)