ItemList.coffee 611 B

1234567891011121314151617181920212223242526
  1. class ItemList
  2. constructor: (@item_class, @key) ->
  3. @items = []
  4. @items_bykey = {}
  5. sync: (rows, item_class, key) ->
  6. @items.splice(0, @items.length) # Empty items
  7. for row in rows
  8. current_obj = @items_bykey[row[@key]]
  9. if current_obj
  10. current_obj.row = row
  11. @items.push current_obj
  12. else
  13. item = new @item_class(row, @)
  14. @items_bykey[row[@key]] = item
  15. @items.push item
  16. deleteItem: (item) ->
  17. index = @items.indexOf(item)
  18. if index > -1
  19. @items.splice(index, 1)
  20. else
  21. console.log "Can't delete item", item
  22. delete @items_bykey[item.row[@key]]
  23. window.ItemList = ItemList