问题
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?
回答1:
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')
回答2:
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?
来源:https://stackoverflow.com/questions/6547430/backbone-js-save-with-coffeescript