backbone.js save with coffeescript

眉间皱痕 提交于 2019-12-12 16:19:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!