【问题标题】:Success method not triggered on Collection fetch with params使用参数获取 Collection 时未触发成功方法
【发布时间】:2014-02-27 16:13:50
【问题描述】:

我是骨干新手,我遇到了一个非常奇怪的问题。问题是当我将参数传递给集合获取时,它永远不会触发成功回调方法,但没有参数它会触发。

这是我的代码,它永远不会成功回调,但如果我删除数据参数,那么它会成功回调并发出警报消息。

P.S,我知道还有另一种方法可以设置reset 属性并将集合绑定到视图中,但我不能使用它。

showPhotoList: (projectId) =>
    @photoLists.fetch data: $.param(project_id: projectId),
      success: @renderPhotoList,

      error: =>
        alert "Error occured while fetching the project"


  renderPhotoList: =>
    alert "hellow render"   

【问题讨论】:

  • 传参时是否触发错误回调?
  • @Puigcerber,不,它没有......但是没有参数,成功和错误都像魅力一样工作
  • 为什么需要$.paramfetchsync 方法会处理它。你只需要data: {project_id: projectId}

标签: javascript ruby-on-rails backbone.js coffeescript


【解决方案1】:

您的 CoffeeScript 并没有像您认为的那样做。这个:

@photoLists.fetch data: $.param(project_id: projectId),
  success: @renderPhotoList,

  error: =>
    alert "Error occured while fetching the project"

真的是这个意思:

@photoLists.fetch(
  { data: ... },
  { success: ..., error: => ... }
)

所以你的successerror 回调在second 参数中被传递给fetch,而不是first 选项参数。 Collection#fetch 只知道一个选项参数,所以它永远不会看到你的回调。

您需要非常小心并与您的 CoffeeScript 格式保持一致:

@photoLists.fetch
  data: $.param(project_id: projectId)
  success: @renderPhotoList
  error: =>
    alert "Error occured while fetching the project"

如果这真的是你的错误回调,那么你就不需要一个胖箭头 (=>),它不在乎什么 @ (AKA this) 是一个未绑定的函数 (@ 987654332@) 就足够了。

【讨论】:

    猜你喜欢
    • 2011-01-23
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    • 2017-10-24
    • 2023-03-23
    • 2011-12-09
    相关资源
    最近更新 更多