backbone.js 用 CoffeeScript 保存

发布于 2024-11-17 23:01:21 字数 556 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-11-24 23:01:21

Backbone.Model.save 需要 2 个参数,第一个是要更改的属性列表,第二个是回调配置。

因此,如果您在保存期间不更改任何其他属性,则可以仅传递一个空对象:

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

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')
孤千羽 2024-11-24 23:01:21

第一个答案对我有用,但略有修改。我必须传递 null,而不是传递空哈希,否则空哈希用于设置模型上的所有属性,替换任何现有属性并实际上删除它们。

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

以上是我的工作内容,也许 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.

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文