metafoo
metafoo

Reputation: 95

Spine.js Model: Only first .save() should create new resource, all following should update

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?

Upvotes: 0

Views: 420

Answers (1)

slarti42uk
slarti42uk

Reputation: 451

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.

Upvotes: 1

Related Questions