将新模型添加到集合时,所有先前模型的所有属性的值都将设置为新模型的值。
以下是完整的源代码:https://github.com/dirty-hippie/chocolate/tree/master/app/assets/javascripts
流程:
1)在第一部分用户选择甜心的形式 - 酒吧,糖果或人物。代码在views / form.coffee
中class App.Views.Form extends Backbone.View
template: HandlebarsTemplates['form']
tagName: 'label',
className: 'product control-as-image'
events:
'click img': 'selectForm'
render: ->
@$el.html(@template(@model.toJSON()))
@el.setAttribute('for', "form-" +@model.get('id'))
@
selectForm: ->
App.Vent.trigger "form:select", {name: @model.get('name'), price: @model.get('price')}
2)在下一节中,用户选择成分:推荐(无线电台)或个人(复选框,最多三个)。代码在views / ingredient.coffee中
class App.Views.Ingredient extends Backbone.View
template: HandlebarsTemplates['ingredient']
tagName: 'label',
className: 'product control-as-image'
attributes:
"data-component" : "ingredient"
events:
'click img': 'selectIngredient'
render: ->
#handlebar сompotability
if @model.get('kind') == 'recipe'
@model.set('isRecipe', true)
else
@model.set('isRecipe', false)
@$el.html(@template(@model.toJSON()))
@el.setAttribute('for', "ingredient-" + @model.get('id'))
@
dataForOrder: ->
name: @model.get('name')
selectIngredient: (event) ->
if @model.get('kind') == 'recipe'
App.Vent.trigger "ingredient:set", @dataForOrder()
else #dealing with compound ingredients, up to three
unless @el.childNodes[1].checked #state before the click
App.Vent.trigger "ingredient:push", @dataForOrder() #checked
else
App.Vent.trigger "ingredient:pop", @dataForOrder() #unchecked
3)将广告按到篮子里。代码位于views / line_item.coffee中 class App.Views.LineItem扩展Backbone.View
template: HandlebarsTemplates['line_item']
events:
'click #send-to-basket' : 'sendToBasket'
initialize: ->
@listenTo App.Vent, 'form:select', @setForm
@listenTo App.Vent, 'ingredient:set', @setIngredient
@listenTo App.Vent, 'ingredient:push', @pushIngredient
@listenTo App.Vent, 'ingredient:pop', @popIngredient
@template = HandlebarsTemplates['order_item'] if @model.get('inBasket')
render: ->
@$el.html(@template(@model.toJSON()))
@
setForm: (form) ->
@model.set('form', form)
setIngredient: (ingredient) ->
@model.set('ingredients', [ingredient])
pushIngredient: (ingredient) ->
ingredients = @model.get('ingredients')
if ingredients.length < 3
ingredients = [] if ingredients[0] and ingredients[0].isRecipe #reset
ingredients.push(ingredient)
@model.set 'ingredients', ingredients
popIngredient: (removedIngredient) ->
ingredients = @model.get('ingredients')
_.remove ingredients, (ingredient) ->
ingredient.name is removedIngredient.name
@model.set 'ingredients', ingredients
sendToBasket: ->
if @model.get('form') isnt null and @model.get('ingredients').length > 0
App.Vent.trigger "order:add", {form: @model.get('form'), ingredients: @model.get('ingredients')}
else
$.pnotify
title: 'Выберите форму и начинку'
4)当甜心被添加到篮子里时,所有以前的甜点都会变成新的甜点。代码在views / order.coffee中
class App.Views.Order extends Backbone.View
renderItem: (model) ->
model.set('inBasket', true)
v = new App.Views.LineItem({model: model})
@$el.find("#line-items").append(v.render().el)
addItem: (item) ->
@collection.add new App.Models.LineItem(item)
console.dir(@collection)
@render()
我甚至开始只发送一些特定属性而不是整个属性,以避免模型ID重复https://github.com/dirty-hippie/chocolate/blob/master/app/assets/javascripts/views/ingredient.js.coffee#L31
以下是工作代码,但所有标签均为俄语http://enigmatic-ridge-6116.herokuapp.com/constructor
任何有用的帮助!
答案 0 :(得分:0)
您的lineitems上的默认值不是基元。尝试使默认值为返回{ingredients:[],form:null}
的函数