最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone.js saving model with attribute that is array of other models - Stack Overflow

programmeradmin2浏览0评论

im writing first open source Backbone.js app. The repository is here

Im having problem with saving array of LineItems for Invoice. Well, only saving after editing, because it saves the line items from currently edited invoice, to all invoices in localstorage. Dunno why this is happening, they have always same Cids. The default line item while creating invoice have always cid0. Any help?

class window.Invoice extends Backbone.Model

  initialize: ->

  defaults:
    date: new Date
    number: '000001'
    seller_info: null
    buyer_info: null  
    line_items: [new LineItem]

The last thing i do not understand is why backbone isnt saving the nested attributes. As you will see in repo i do:

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 = "#"

The things is after editing form and changing values of line items, they do not change in collection. Adding new to invoice line items collection works. Any help? :) I have some hard time with understanding how everyhing works :)

You can check it here: /

im writing first open source Backbone.js app. The repository is here https://github./defrag/Backbone-Invoices

Im having problem with saving array of LineItems for Invoice. Well, only saving after editing, because it saves the line items from currently edited invoice, to all invoices in localstorage. Dunno why this is happening, they have always same Cids. The default line item while creating invoice have always cid0. Any help?

class window.Invoice extends Backbone.Model

  initialize: ->

  defaults:
    date: new Date
    number: '000001'
    seller_info: null
    buyer_info: null  
    line_items: [new LineItem]

The last thing i do not understand is why backbone isnt saving the nested attributes. As you will see in repo i do:

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 = "#"

The things is after editing form and changing values of line items, they do not change in collection. Adding new to invoice line items collection works. Any help? :) I have some hard time with understanding how everyhing works :)

You can check it here: http://backbone-invoices.brillante.pl/

Share Improve this question edited Sep 15, 2011 at 21:08 Jeff Atwood 64k48 gold badges151 silver badges153 bronze badges asked Sep 14, 2011 at 10:20 MichalMichal 6181 gold badge5 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

default values are literal values, evaluated at the time of definition. this means that you are assigning the same instance of LineItem to the array, for every instance of Invoice.

The fix for this is simple: use a function to return the array. this way you get a new array of line items every time you create an invoice:

window.Invoice = Backbone.Model.extend({
  defaults: {
    date: function(){ return new Date(); },
    line_items: function(){ return [new LineItem()]; },
    other: "stuff"
  }
});
发布评论

评论列表(0)

  1. 暂无评论