backbone.js save with coffeescript

2019-07-06 03:06发布

I have the following method on a backbone view defined in coffeescript:

  saveObservation: =>
    self = @
    observation = new Observation(ParentUid: _questionUid, Status: "N/a", Text: "Change to element")
    observation.save {
          success: ->
            alert('test')
          error: ->
            alert('failed')
        }

Observation is extended from Backbone.Model

class Observation extends Backbone.Model
  url: ->
    "/AuditActionTracking/"  

The save reaches the server but neither the success nor the error handlers I have defined in the save are getting called after the ajax call has completed.

Can anyone see what I am doing wrong?

2条回答
\"骚年 ilove
2楼-- · 2019-07-06 03:38

Backbone.Model.save takes 2 parameters, the first is a list of properties you're changing, and the second is the callback configuration.

So, if you're not changing any other properties during save, you can just pass an empty object:

observation.save {},
    success: (model, response) ->
      alert('test')
    error: (model, response) ->
      alert('failed')
查看更多
一纸荒年 Trace。
3楼-- · 2019-07-06 03:56

The first answer worked for me but with a slight modification. Instead of passing in an empty hash I had to pass in null, otherwise the empty hash is used to set all attributes on the model, replacing any existing attributes and in effect deleting them.

observation.save null,
  success: (model, response) ->
    alert('test')
  error: (model, response) ->
    alert('failed')

The above is what worked from me, perhaps the api changed since this previous answer was posted?

查看更多
登录 后发表回答