Skip to content

Instantly share code, notes, and snippets.

@oleglukashev
Created August 31, 2016 19:53
Show Gist options
  • Save oleglukashev/33cfee72fe844b8450e14b16abc3b0f6 to your computer and use it in GitHub Desktop.
Save oleglukashev/33cfee72fe844b8450e14b16abc3b0f6 to your computer and use it in GitHub Desktop.
backbone2
Cooking.Views.CashPositions ||= {}
class Cooking.Views.CashPositions.IndexView extends Backbone.View
template: JST["backbone/templates/cash_positions/index"]
model: Cooking.Models.CashPosition
initialize: () ->
that = @
@collection.bind 'reset', @buildAll
@collection.on('add', (cash_position) ->
that.buildCashPosition(cash_position)
)
@collection.on('remove', (cash_position) ->
that.removeCashPosition(cash_position)
)
@listenTo Backbone, 'CashPositionsIndex:cash_position:change', (cash_position) ->
that.reBuildCashPosition(cash_position)
that.removeExcessGivenCashPositions()
@cash_positions_groups_view = {}
@cash_positions_view = {}
@cash_tables_view = {}
@cash_adds_view = {}
@dish_groups_view = {}
@dish_group = false
buildAll: () =>
that = @
_.each(@collection.statuses, (status) ->
that.buildGroup(status)
)
buildGroup: (group) =>
that = @
@cash_positions_groups_view[group] = new Cooking.Views.CashPositions.CashPositionsGroupView({status: group})
@$('.cash-positions__groups').append @cash_positions_groups_view[group].render().el
@buildCashPositionsByGroup(group)
buildCashPositionsByGroup: (group) ->
that = @
if group is "given"
grouped_cash_positions = _(@collection.where({status: group})).sortBy((cash_position) ->
cash_position.get("updated_at")
)
grouped_cash_positions = _(grouped_cash_positions).last(10)
_.each( grouped_cash_positions, (cash_position) ->
that.buildCashPosition(cash_position)
)
else
_.each(@collection.where({status: group}), (cash_position) ->
that.buildCashPosition(cash_position)
)
reBuildCashPosition: (cash_position) =>
@removeCashPosition(cash_position)
@buildCashPosition(cash_position)
removeCashPosition: (cash_position) =>
if @cash_positions_view[cash_position.get('id')]
@cash_positions_view[cash_position.get('id')].remove()
removeExcessGivenCashPositions: () =>
that = @
given_cash_positions = _(@collection.where({status: "given"})).sortBy((cash_position) ->
cash_position.get("updated_at")
).reverse()
if given_cash_positions.length > 10
_(given_cash_positions).each (cash_position, index) ->
if index >= 10
that.removeCashPosition(cash_position)
buildCashPosition: (cash_position) =>
status = cash_position.get('status')
@cash_positions_view[cash_position.get('id')] = new Cooking.Views.CashPositions.CashPositionView({
model : cash_position
})
list = @$('.cash-positions-group--' + status + ' .cash-positions-group__list')
if cash_position.get('status') is "given"
list.prepend(@cash_positions_view[cash_position.get('id')].render().el)
else
list.append(@cash_positions_view[cash_position.get('id')].render().el)
render: =>
@$el.html(@template(cashPositions: @collection.toJSON() ))
@buildAll()
return this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment