【发布时间】:2011-09-16 05:08:54
【问题描述】:
我正在编写第一个开源 Backbone.js 应用程序。 仓库在这里https://github.com/defrag/Backbone-Invoices
我在为 Invoice 保存 LineItems 数组时遇到问题。好吧,只在编辑后保存,因为它将当前编辑的发票中的行项目保存到本地存储中的所有发票中。不知道为什么会这样,他们总是有相同的 Cids。 创建发票时的默认行项目始终为 cid0。有什么帮助吗?
class window.Invoice extends Backbone.Model
initialize: ->
defaults:
date: new Date
number: '000001'
seller_info: null
buyer_info: null
line_items: [new LineItem]
我不明白的最后一件事是为什么主干不保存嵌套属性。正如您将在 repo 中看到的那样:
handleSubmit: (e) ->
data = {
date : @$("input[name='date']").val(),
number : @$("input[name='number']").val(),
buyer_info : @$("textarea[name='buyer_info']").val(),
seller_info : @$("textarea[name='seller_info']").val(),
line_items: @model.line_items.toJSON()
}
if @model.isNew()
invoices.create(data)
else
@model.save(data)
e.preventDefault()
e.stopPropagation()
$(@el).fadeOut 'fast', ->
window.location.hash = "#"
事情是在编辑表单和更改行项目的值之后,它们在集合中没有变化。向发票行项目集合添加新功能。有什么帮助吗? :) 我很难理解一切是如何运作的 :)
【问题讨论】:
标签: javascript backbone.js coffeescript