Files
mozo-backend/app/assets/javascripts/user/app/controllers/application_controller.js.coffee
T
2014-03-31 17:43:51 +02:00

70 lines
2.9 KiB
CoffeeScript

App.ApplicationController = Ember.Controller.extend
notice: ''
actions:
confirmCancel: ->
if cancel = @get('confirm.cancel')
cancel.call(@)
$('#confirm-modal').hide()
clearNotice: ->
@set 'notice', null
openDebugger: ->
debugger
currentPathDidChange: (->
@set 'notice', ''
).observes('currentPath')
events:
list_helped: -> @set 'list.needs_help', false
list_needs_help: -> @set 'list.needs_help', true # incoming from other users
list_is_paid: -> @set 'list.needs_payment', false
list_needs_payment: -> @set 'list.needs_payment', true # incoming from other users
list_closed: (data)->
@transitionToRoute('list', data.id).then =>
@set 'list.state', 'closed'
@set 'list', null
join_request_approved: (data)->
@setCurrentList -> @transitionToRoute('active_list')
order_being_processed: (data)->
order = @store.all('order').findBy 'id', data.id
order.set('state', 'active') if order
order_being_delivered: (data)->
order = @store.all('order').findBy 'id', data.id
order.set('state', 'delivered') if order
user_join_request: (data)->
list = @store.all('list').findBy 'id', data.join_request.list_id
user_data = data.users[0]
user = @store.all('user').findBy 'id', user_data.id
user = @store.createRecord 'user', user_data unless user
data.join_request.list = list
data.join_request.user = user
join_request = @store.createRecord 'join_request', data.join_request
@transitionToRoute 'join_requests'
join_request_rejected: (data)->
join_request = @store.all('join_request').findBy 'id', data.id
join_request.eraseRecord() if join_request
join_request_approved: ->
@setCurrentList ->
@transitionToRoute 'active_list'
#getProducts: (options = {})->
#@store.find('product_category', options).then (product_categories)=>
#@controllerFor('list_products').set 'model', product_categories
setCurrentList: (callback)->
success = (list)=>
#@store.find('list', 'current').deleteRecord() # gets not replaced, buty stays as dummy
# A list record with id current and with the content of the returned list is created
# at the moment remove the dummy list, this should be resolved by Ember eventually
if error_list = @store.all('list').findBy('id', 'current')
error_list.eraseRecord()
@set 'list', list
@controllerFor('active_list').set('model', list)
if list.get('join_requests').toArray().length
@transitionToRoute 'join_requests'
callback.call(@) if callback
error = (emberError)=>
# if jqXHR.status == 404 officially, now assume close list on error
#@redirect_to 'index', message: 'the_list_has_been_closed'
console.log "Error: #{emberError.message}" if emberError.message
@set 'list', null
@store.find('list', 'current').then(success, error)