backbone.js 用 CoffeeScript 保存
我在 CoffeeScript 中定义的主干视图上有以下方法:
saveObservation: =>
self = @
observation = new Observation(ParentUid: _questionUid, Status: "N/a", Text: "Change to element")
observation.save {
success: ->
alert('test')
error: ->
alert('failed')
}
观察是从 Backbone.Model 扩展的
class Observation extends Backbone.Model
url: ->
"/AuditActionTracking/"
保存到达服务器,但在 ajax 调用完成后,我在保存中定义的成功和错误处理程序都不会被调用。
谁能看到我做错了什么吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Backbone.Model.save 需要 2 个参数,第一个是要更改的属性列表,第二个是回调配置。
因此,如果您在保存期间不更改任何其他属性,则可以仅传递一个空对象:
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:
第一个答案对我有用,但略有修改。我必须传递 null,而不是传递空哈希,否则空哈希用于设置模型上的所有属性,替换任何现有属性并实际上删除它们。
以上是我的工作内容,也许 api 自从发布之前的答案后发生了变化?
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.
The above is what worked from me, perhaps the api changed since this previous answer was posted?