0

When I create and .save() a new Record, my Spine.js Model sends a POST Request to the server to add it to the database. No big deal... But when I issue another .save() to the same model instance I want Spine.js to send a PUT Request just updating it's data.

How can I achieve this?

This is how my model looks like:

class App.MeetingMinutes extends Spine.Model
  @configure 'MeetingMinutes', 'subject', 'some', 'more', 'attributes'
  @belongsTo 'archive', 'Archive'
  @extend Spine.Model.Ajax

  @url: '#' # only dynamically scoped from Entries controller

  @setArchive: ( _id ) ->
    @url = '/archives/' + _id + '/meeting_minutes'

(Adding an id column to @configure doesn't work either)

In my Rails controller (create action) I return the whole Object as JSON

format.json { render :json => @meeting_minutes, :status => :created }

And that's what I'm using it:

mm = new App.MeetingMinutes({subject: 'subject'})
mm.save() // POST
mm.subject = 'changed subject!'
mm.save() // another POST

mm.isNew() // true (obviously :/)

Any ideas?

metafoo
  • 95
  • 1
  • 8
  • 1
    The way you describe it is how it works for me. Can you check what isNew() returns on the model after the first save? This should be false. If it's true it'll issue another POST. Do you have an ID column on your model? – SpoBo Apr 07 '12 at 16:28
  • Thanks for your reply! I just added some more information to my posting. – metafoo Apr 08 '12 at 10:08
  • If I was you I'd add a debugger before the first save and step through all of it. You need to make sure that changeID is called in spine to set the ID of the mm record to the ID generated on the server. As soon as that happens the record should no longer be marked as isNew(). Just step through it and see if it happens or not. And if not, why. – SpoBo Apr 08 '12 at 13:43

1 Answers1

1

Not sure if you got this sorted but it sounded like an issue I had faced a little while back. I think you need to assign the save to the variable, so this:

mm = new App.MeetingMinutes({subject: 'subject'})
mm = mm.save()
mm.subject = 'changed subject!'
mm = mm.save()

The second save should then be a PUT not a POST. Now I'm not clear if this is the intended usage or just a work around but it seems to work. Hope it helps.

slarti42uk
  • 451
  • 3
  • 9