user ember progress

This commit is contained in:
2014-03-26 17:48:54 +01:00
parent 4e75c72097
commit 6af7dc74af
19 changed files with 129 additions and 35 deletions
@@ -0,0 +1,10 @@
App.ActiveListController = Ember.ArrayController.extend
orders: (->
@get('list.orders')
).property('list.orders')
list: (->
@get('controllers.application.list')
).property('controllers.application.list')
displayTotal: (-> @get('list.orders.length') and @get('list.orders.length') > 1 ).property('list.orders.length')
@@ -18,13 +18,6 @@ App.ApplicationController = Ember.Controller.extend
list_is_paid: -> @set 'list.needs_payment', false
list_needs_payment: -> @set 'list.needs_payment', true # incoming from other users
getList: ->
Ember.$.get('/user/list_info.json').then (res)=>
if res.not_present
@set 'list', null
else
@set 'list', App.List.create(res)
@getProducts table_id: @get('list.table_id')
getProducts: (options = {})->
@store.find('product_category', options).then (product_categories)=>
@controllerFor('list_products').set 'model', product_categories
#getProducts: (options = {})->
#@store.find('product_category', options).then (product_categories)=>
#@controllerFor('list_products').set 'model', product_categories
@@ -1,7 +1,7 @@
App.ListProductsController = Ember.ArrayController.extend
actions:
addProduct: (product)->
if existing = @store.all('product_order').find((po)-> po.get('product') == product)
if existing = @store.all('product_order').find((po)-> po.get('product') == product and not po.get('order'))
existing.increment()
else
@store.createRecord 'product_order', product: product
@@ -7,15 +7,24 @@ App.ProductOrdersController = Ember.ArrayController.extend
clearProductOrders: ->
@store.all('product_order').toArray().invoke 'unloadRecord'
orderProducts: ->
orders = @store.all('product_order').toArray()
data = orders.map( (order)->order.serialize() )
dataObject = {order: {}}
for product_order in data
dataObject['order'][product_order.product_id] = product_order.quantity
$.ajax
url: Routes.user_order_selected_products_path()
type: "POST",
data: JSON.stringify(dataObject),
contentType: "application/json",
dataType: 'json'
orders.invoke 'unloadRecord'
order = @store.createRecord('order', list: @get('controllers.application.list'))
new_product_orders = @store.all('product_order').filterProperty('order', null)
new_product_orders.forEach (po) -> po.setOrder(order)
order.save()
@transitionToRoute 'active_list'
#orders = @store.all('product_order').toArray()
#data = orders.map( (order)->order.serialize() )
#dataObject = {order: {}}
#for product_order in data
#dataObject['order'][product_order.product_id] = product_order.quantity
##$.ajax
##url: Routes.user_order_selected_products_path()
##type: "POST",
##data: JSON.stringify(dataObject),
##contentType: "application/json",
##dataType: 'json'
#orders.invoke 'unloadRecord'
removeProductOrder: (product_order)->
product_order.unloadRecord()
@@ -1 +1,8 @@
App.List = Ember.Object.extend {}
attr = DS.attr
App.List = DS.Model.extend
orders: DS.hasMany('order')
needs_help: attr('boolean')
needs_payment: attr('boolean')
total: (->
@get('orders').getEach('total').reduce(((sum, total) -> sum + total), 0)
).property('orders.@each.total')
@@ -0,0 +1,11 @@
attr = DS.attr
App.Order = DS.Model.extend
state: attr 'string'
list: DS.belongsTo('list')
product_orders: DS.hasMany('product_order', embedded: 'always')
total: (->
@get('product_orders').getEach('total').reduce(((sum, total) -> sum + total), 0)
).property('product_orders.@each.quantity')
display: (->
@get('product_orders').map((po) -> "#{po.get('quantity')} x #{po.get('product.name')}").join(', ')
).property('product_orders.@each.quantity')
@@ -2,6 +2,13 @@ attr = DS.attr
App.ProductOrder = DS.Model.extend
quantity: attr 'number', defaultValue: 1
product: DS.belongsTo('product')
order: DS.belongsTo('order')
placed: attr('boolean', defaultValue: false)
increment: ->
@set('quantity', @get('quantity') + 1)
total: (-> @get('quantity') * @get('product.price')).property('quantity')
setOrder: (order)->
@set('placed', true)
@set('order', order)
@@ -0,0 +1,3 @@
App.ActiveListRoute = Ember.Route.extend
model: ->
@controllerFor('application').get('list.orders')
@@ -1,13 +1,15 @@
App.ApplicationRoute = Ember.Route.extend
setupController: (controller)->
@controllerFor('product_orders').set 'model', @store.all('product_order')
#@controllerFor('product_orders').set 'model', @store.filter('product_order', (po)-> !po.get('order')) # does not work (yet)
@controllerFor('product_orders').set 'model', @store.filter('product_order', (po)-> !po.get('placed'))
controller.secured ->
faye = new Faye.Client(event_host)
user_id = Qstorage.getItem('user_id')
faye.subscribe "/user/"+user_id, (e)=>
console.log e
@events[e.event].call(@) if @events[e.event]
@getList()
@store.find('list', 'current').then (list)=>
@set 'list', list
unauthorized: ->
Qstorage.setItem('auth_token', '')
@@ -1,4 +1,6 @@
App.ListProductsRoute = Ember.Route.extend {}
App.ListProductsRoute = Ember.Route.extend
model: ->
@store.all 'product_category'
#setupController: (controller)->
#controller.secured ->
#src = '/user/list_products.json'
@@ -1 +1,19 @@
h2 Active list
.row
h2=t 'active_list.title'
if list.orders
ul.active_list-orders
each order in list.orders
li class=order.state
= order.display
span.currency= currency order.total
if displayTotal
li.total
= t 'total'
span.currency= currency list.total
else
p
span=t 'active_list.no_orders_explanation'
br
link-to 'list_products' class="button"
span=t 'list_products.title'
@@ -1,9 +1,13 @@
.row
.large-6.columns
each product_category in controller
hr
h4= product_category.name
hr
each product in product_category.products
a{action addProduct product}= product.name
if product_category.products
hr
h4= product_category.name
hr
ul.product_category-products
each product in product_category.products
li
a{action addProduct product}= product.name
span.right.currency=currency product.price
.large-6.columns= render 'product_orders'
@@ -1,6 +1,6 @@
hr.hide-for-medium-up
if model
a.tiny.button.right{action clearProductOrders} href="#" x
a.tiny.button.right{action clearProductOrders} href="#" ×
.clearfix
.panel
ul.product-orders
@@ -9,11 +9,13 @@ if model
= product_order.quantity
| x
= product_order.product.name
a.product_order-remove.right{action removeProductOrder product_order}
span.fa.fa-close.fa-lg x
span.currency=currency product_order.total
else
li= t 'product_orders.no_orders'
li.total
= t 'product_orders.total'
span.currency=currency orderTotal
span.right.currency=currency orderTotal
if model
a.tiny.button.right{action orderProducts} href="#"= t 'product_orders.order_button'
@@ -6,3 +6,8 @@ ul.product-orders
border-bottom: none
border-top: 4px solid #333
font-weight: bold
.product_order-remove
color: black
background-color: #bbb
padding: 0px 5px
margin-left: 5px
@@ -0,0 +1,2 @@
ul.product_category-products
list-style: none