src/components/collection_view.coffee in luca-0.9.2 vs src/components/collection_view.coffee in luca-0.9.4

- old
+ new

@@ -1,6 +1,14 @@ -make = Luca.View::make +# Public: The CollectionView renders a collection of models into a list of items. +# +# Examples +# +# _.def('App.ListView').extends('Luca.components.CollectionView').with +# collection: new App.SampleCollection() +# itemTemplate: "list_view_item" +# loadMask: true +# _.def("Luca.components.CollectionView").extends("Luca.components.Panel").with tagName: "div" className: "luca-ui-collection-view" @@ -15,10 +23,14 @@ itemTagName: 'li' itemClassName: 'collection-item' + hooks:[ + "empty:results" + ] + initialize: (@options={})-> _.extend(@, @options) _.bindAll @, "refresh" @@ -29,58 +41,74 @@ throw "Collection Views must specify an item template or item renderer function" Luca.components.Panel::initialize.apply(@, arguments) if _.isString(@collection) and Luca.CollectionManager.get() - @collection = Luca.CollectionManager.get().get(@collection) + @collection = Luca.CollectionManager.get().getOrCreate(@collection) if Luca.isBackboneCollection(@collection) - @collection.bind "reset", @refresh + @collection.on "before:fetch", ()=> + @trigger "enable:loadmask" if @loadMask is true + + @collection.bind "reset", ()=> + @trigger "disable:loadmask" if @loadMask is true + @refresh() + @collection.bind "add", @refresh @collection.bind "remove", @refresh + else + throw "Collection Views must have a valid backbone collection" + if @collection.length > 0 + @refresh() + attributesForItem: (item)-> _.extend {}, class: @itemClassName, "data-index": item.index contentForItem: (item={})-> if @itemTemplate? and templateFn = Luca.template(@itemTemplate) content = templateFn.call(@, item) if @itemRenderer? and _.isFunction( @itemRenderer ) - content = @itemRenderer.call(@, item) + content = @itemRenderer.call(@, item, item.model, item.index) if @itemProperty content = item.model.get(@itemProperty) || item.model[ @itemProperty ] content = content() if _.isFunction(content) content makeItem: (model, index)-> item = if @prepareItem? then @prepareItem.call(@, model, index) else (model:model, index: index) - make(@itemTagName, @attributesForItem(item), @contentForItem(item)) getModels: ()-> if @collection?.query and (@filter || @filterOptions) @collection.query(@filter, @filterOptions) else @collection.models refresh: ()-> - panel = @ - @$bodyEl().empty() - _( @getModels() ).each (model, index)-> - panel.$append( panel.makeItem(model, index) ) + if @getModels().length is 0 + @trigger("empty:results") + _( @getModels() ).each (model, index)=> + @$append( @makeItem(model, index) ) + registerEvent: (domEvent, selector, handler)-> if !handler? and _.isFunction(selector) handler = selector selector = undefined eventTrigger = _([domEvent,"#{ @itemTagName }.#{ @itemClassName }", selector]).compact().join(" ") Luca.View::registerEvent(eventTrigger,handler) render: ()-> @refresh() - @$attach() if @$el.parent().length > 0 and @container? \ No newline at end of file + @$attach() if @$el.parent().length > 0 and @container? + @ + +# Private Helpers + +make = Luca.View::make