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_is_paid: -> @set 'list.needs_payment', false
list_needs_payment: -> @set 'list.needs_payment', true # incoming from other users list_needs_payment: -> @set 'list.needs_payment', true # incoming from other users
getList: -> #getProducts: (options = {})->
Ember.$.get('/user/list_info.json').then (res)=> #@store.find('product_category', options).then (product_categories)=>
if res.not_present #@controllerFor('list_products').set 'model', product_categories
@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
@@ -1,7 +1,7 @@
App.ListProductsController = Ember.ArrayController.extend App.ListProductsController = Ember.ArrayController.extend
actions: actions:
addProduct: (product)-> 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() existing.increment()
else else
@store.createRecord 'product_order', product: product @store.createRecord 'product_order', product: product
@@ -7,15 +7,24 @@ App.ProductOrdersController = Ember.ArrayController.extend
clearProductOrders: -> clearProductOrders: ->
@store.all('product_order').toArray().invoke 'unloadRecord' @store.all('product_order').toArray().invoke 'unloadRecord'
orderProducts: -> orderProducts: ->
orders = @store.all('product_order').toArray() order = @store.createRecord('order', list: @get('controllers.application.list'))
data = orders.map( (order)->order.serialize() ) new_product_orders = @store.all('product_order').filterProperty('order', null)
dataObject = {order: {}} new_product_orders.forEach (po) -> po.setOrder(order)
for product_order in data order.save()
dataObject['order'][product_order.product_id] = product_order.quantity @transitionToRoute 'active_list'
$.ajax
url: Routes.user_order_selected_products_path() #orders = @store.all('product_order').toArray()
type: "POST", #data = orders.map( (order)->order.serialize() )
data: JSON.stringify(dataObject), #dataObject = {order: {}}
contentType: "application/json", #for product_order in data
dataType: 'json' #dataObject['order'][product_order.product_id] = product_order.quantity
orders.invoke 'unloadRecord' ##$.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 App.ProductOrder = DS.Model.extend
quantity: attr 'number', defaultValue: 1 quantity: attr 'number', defaultValue: 1
product: DS.belongsTo('product') product: DS.belongsTo('product')
order: DS.belongsTo('order')
placed: attr('boolean', defaultValue: false)
increment: -> increment: ->
@set('quantity', @get('quantity') + 1) @set('quantity', @get('quantity') + 1)
total: (-> @get('quantity') * @get('product.price')).property('quantity') 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 App.ApplicationRoute = Ember.Route.extend
setupController: (controller)-> 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 -> controller.secured ->
faye = new Faye.Client(event_host) faye = new Faye.Client(event_host)
user_id = Qstorage.getItem('user_id') user_id = Qstorage.getItem('user_id')
faye.subscribe "/user/"+user_id, (e)=> faye.subscribe "/user/"+user_id, (e)=>
console.log e console.log e
@events[e.event].call(@) if @events[e.event] @events[e.event].call(@) if @events[e.event]
@getList() @store.find('list', 'current').then (list)=>
@set 'list', list
unauthorized: -> unauthorized: ->
Qstorage.setItem('auth_token', '') 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)-> #setupController: (controller)->
#controller.secured -> #controller.secured ->
#src = '/user/list_products.json' #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 .row
.large-6.columns .large-6.columns
each product_category in controller each product_category in controller
hr if product_category.products
h4= product_category.name hr
hr h4= product_category.name
each product in product_category.products hr
a{action addProduct product}= product.name 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' .large-6.columns= render 'product_orders'
@@ -1,6 +1,6 @@
hr.hide-for-medium-up hr.hide-for-medium-up
if model if model
a.tiny.button.right{action clearProductOrders} href="#" x a.tiny.button.right{action clearProductOrders} href="#" ×
.clearfix .clearfix
.panel .panel
ul.product-orders ul.product-orders
@@ -9,11 +9,13 @@ if model
= product_order.quantity = product_order.quantity
| x | x
= product_order.product.name = 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 span.currency=currency product_order.total
else else
li= t 'product_orders.no_orders' li= t 'product_orders.no_orders'
li.total li.total
= t 'product_orders.total' = t 'product_orders.total'
span.currency=currency orderTotal span.right.currency=currency orderTotal
if model if model
a.tiny.button.right{action orderProducts} href="#"= t 'product_orders.order_button' a.tiny.button.right{action orderProducts} href="#"= t 'product_orders.order_button'
@@ -6,3 +6,8 @@ ul.product-orders
border-bottom: none border-bottom: none
border-top: 4px solid #333 border-top: 4px solid #333
font-weight: bold 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
+12
View File
@@ -0,0 +1,12 @@
module Users
class ListsController < Users::ApplicationController
#EMBER
def current
list = current_user.active_list
render json: json_response(not_present: true) and return unless list.present?
render json: {
list: list.serialized_with_status_join_requests_and_supplier_counters,
}.merge(ActiveModel::ArraySerializer.new(list.supplier.product_categories, each_serializer: ProductCategorySerializer, root: :product_categories).as_json)
end
end
end
+1
View File
@@ -23,6 +23,7 @@ en:
active_list: active_list:
title: Active list title: Active list
needs_payment: Check please! needs_payment: Check please!
no_orders_explanation: You did not order anything
list_products: list_products:
title: Order title: Order
history_list: history_list:
+1
View File
@@ -22,6 +22,7 @@ nl:
active_list: active_list:
title: Actieve lijst title: Actieve lijst
needs_payment: Rekening vragen! needs_payment: Rekening vragen!
no_orders_explanation: Je hebt nog niks besteld
list_products: list_products:
title: Bestel title: Bestel
history_list: history_list:
+5
View File
@@ -71,6 +71,11 @@ Qwaiter::Application.routes.draw do
namespace :users, path: '/user' do namespace :users, path: '/user' do
resources :product_categories, only: [:index] resources :product_categories, only: [:index]
resources :lists, only: [] do
collection do
get :current
end
end
end end