Continue adding product variants

This commit is contained in:
2015-04-13 17:57:06 +02:00
parent 6746eaa383
commit 2b036368cf
32 changed files with 169 additions and 12224 deletions
@@ -4,11 +4,13 @@ App.MenuProductComponent = Ember.Component.extend
specific_id: (-> "order-product-#{@get('product.id')}").property('product.id')
orderProducts: false
target: -> @get('parentView.targetObject')
showDescriptionIcon: Ember.computed.or 'product.description', 'product.product_variants.length'
actions:
addProduct: (product)->
if existing = @target().store.all('product_order').find((po)-> po.get('product') == product and not po.get('order'))
existing.increment()
if product.get('product_variants.length')
@target().modal 'product_variant_select', model: product
else
@target().store.createRecord 'product_order', product: product, price: product.get('price')
product.addOrderItem()
showProductDescription: (product)->
@target().modal 'product_info', model: product, title: product.get('name')
@@ -0,0 +1,5 @@
@App.modals.ProductVariantSelectController = @App.modals.BaseController.extend
actions:
chooseProductVariant: (product_variant)->
@get('model').addOrderItem(product_variant: product_variant.get('name'))
@send 'close'
@@ -31,10 +31,17 @@ App.ProductOrdersController = Ember.ArrayController.extend
#orders = @store.all('product_order').toArray()
#data = @get('product_orders').map( (po)->po.serialize() )
dataObject = order: {table_id: @get('controllers.table.model.id')}
@get('product_orders').forEach (product_order)-> dataObject['order'][product_order.get('product.id')] = product_order.get('quantity')
Ember.$.post "#{$data_host}/user/orders.json", dataObject, (response) =>
@store.pushPayload('order', response) if response.order
dataObject = {table_id: @get('controllers.table.model.id')}
dataObject.product_orders = @get('product_orders').map( (po) -> po.serialize()).toArray()
#@get('product_orders').forEach (product_order)-> dataObject['order'][product_order.get('product.id')] = product_order.get('quantity')
Ember.$.ajax
type: 'POST'
dataType: 'json'
contentType: 'application/json'
url: "#{$data_host}/user/orders.json"
data: JSON.stringify(dataObject)
success: (response) =>
@store.pushPayload('order', response) if response.order
@transitionToRoute 'active_list'
@get('product_orders').invoke 'unloadRecord'
@@ -0,0 +1,5 @@
attr = DS.attr
App.ProductVariant = DS.Model.extend
name: attr 'string'
product: DS.belongsTo 'product'
position: attr 'number', defaultValue: 0
@@ -6,5 +6,26 @@ App.Product = DS.Model.extend
position: attr('number', defaultValue: 0)
active: attr 'boolean', defaultValue: true
image: attr()
product_category: DS.belongsTo('product_category')
product_orders: DS.hasMany('product_order')
product_category: DS.belongsTo('product-category')
product_orders: DS.hasMany('product-order')
product_variants: DS.hasMany 'product-variant'
sorted_product_variants: Ember.computed 'product_variants.@each.name', 'product_variants.@each.position', ->
@get('product_variants').sortBy('position')
variantsDisplay: Ember.computed 'sorted_product_variants', ->
@get('sorted_product_variants').mapBy('name').join(', ')
addOrderItem: (options = {})->
#if existing = @store.all('product_order').find((po)-> po.get('product') == product and not po.get('order'))
if options.product_variant
existing = @get('product_orders').find( (po)-> !po.get('order') and po.get('product_variant') is options.product_variant )
else
existing = @get('product_orders').find( (po)-> !po.get('order') )
if existing
existing.increment()
else
@store.createRecord 'product_order',
product: this
price: @get('price')
product_variant: options.product_variant
@@ -4,12 +4,17 @@ App.ProductOrder = DS.Model.extend
price: attr 'number'
product_name: attr('string')
product: DS.belongsTo('product')
product_variant: attr('string')
order: DS.belongsTo('order')
placed: attr('boolean', defaultValue: false) # virtual attribute for new orders to be placed, should be more elegant
increment: ->
@set('quantity', @get('quantity') + 1)
total: (-> @get('quantity') * @get('price')).property('quantity', 'price')
display: (-> "#{@get('quantity')} x #{@get('product.name')}").property('quantity', 'product.name')
display: Ember.computed 'quantity', 'product_variant', 'product.name', ->
disp = "#{@get('quantity')} x #{@get('product.name')}"
if variant = @get('product_variant')
disp = "#{disp} (#{variant})"
disp.htmlSafe()
setOrder: (order)->
@set('placed', true)
@set('order', order)
@@ -1,4 +1,4 @@
if product.description
if showDescriptionIcon
button.show-product-description{action "showProductDescription" product}
span
else
@@ -1,5 +1,10 @@
if image
.right: img src=image.small alt=""
p==description
if product_variants
h4= t 'models.plural.product_variant'
ul
each product_variant in product_variants
li= product_variant.name
hr
button{action "close"}= t 'modal.info.close'
@@ -0,0 +1,8 @@
if model.image
.right: img src=model.image.small alt=""
each product_variant in product_variants
.row
.small-8.columns: a{action "chooseProductVariant" product_variant}= product_variant.name
.small-4.columns: a.choose-product-variant-button{action "chooseProductVariant" product_variant}= t 'product_variant.choose'
hr
button.modal-close{action "close"}= t 'modal.info.close'