model.fetch 成功回调不会在 Firefox 上触发,但可以在 chrome 上运行

发布于 2024-11-26 14:52:16 字数 1080 浏览 1 评论 0原文

我有一个模型获取的成功回调,并且在 Chrome 上一切正常,但在 Firefox 上该事件不会触发。但根据控制台,请求已完成。

代码示例:

父类功能:

DownloadUserPromotions: (callback) ->
    self = @
    @model = new app.models.client({ id: JSON.parse($.cookie('jsondata')).id })

    lm = ->
      console.log "4"
      window.USER = self.model
      if typeof callback == 'function' then callback.call()   

    @model.fetch
      success: lm
      data: 
        relationships: 'client_promotions'
    console.log "3"

查看功能:

render: ->      
    self = @
    self.ReadUserInfo()
    console.log "1"
    renderTemplate = ->
      console.log "5"
      #Below Issue is wierd.......#TODO
      @USER = JSON.parse(JSON.stringify(@USER))
      $(self.el).html clientsPromotionsTemplate
        promos: USER.client_promotions
      $('.spinner#load').hide()
      self.FadeIn()

    $('.spinner#load').show()
    console.log "2"
    @DownloadUserPromotions renderTemplate  
    @

旁注: 标记的 TODO 是一个不同的问题。另外感谢您帮助我弄清楚为什么 JSON 只能以这种复杂的方式工作。

I have a success callback for a model fetch, and everything works fine on chrome, but on firefox the event does not fire. The request gets completed though, according to the console.

Code Sample:

Parent Class Function:

DownloadUserPromotions: (callback) ->
    self = @
    @model = new app.models.client({ id: JSON.parse($.cookie('jsondata')).id })

    lm = ->
      console.log "4"
      window.USER = self.model
      if typeof callback == 'function' then callback.call()   

    @model.fetch
      success: lm
      data: 
        relationships: 'client_promotions'
    console.log "3"

View Function:

render: ->      
    self = @
    self.ReadUserInfo()
    console.log "1"
    renderTemplate = ->
      console.log "5"
      #Below Issue is wierd.......#TODO
      @USER = JSON.parse(JSON.stringify(@USER))
      $(self.el).html clientsPromotionsTemplate
        promos: USER.client_promotions
      $('.spinner#load').hide()
      self.FadeIn()

    $('.spinner#load').show()
    console.log "2"
    @DownloadUserPromotions renderTemplate  
    @

Side Note: The marked TODO is a different issue. Bonus thank yous for helping me figure out why JSON works only in that convoluted manner.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

尐籹人 2024-12-03 14:52:16

首先,您需要弄清楚 => 之间的区别和->用于在 CoffeeScript 中定义函数。

=>将函数内的 this 绑定到函数定义时的this

->将函数内的 this 绑定到函数被调用时的 this

self = this  

是咖啡脚本中您不明白如何使用的代码气味标志当您尝试捕获this以解决 => 的问题时,请正确执行上述操作解决。

您重写的渲染函数可能

render: ->
    @ReadUserInfo()
    console.log "1"
    renderTemplate = =>
      console.log "5"
      #Below Issue is wierd.......#TODO
      @USER = JSON.parse(JSON.stringify(@USER))
      $(@.el).html clientsPromotionsTemplate
        promos: USER.client_promotions
      $('.spinner#load').hide()
      @.FadeIn()

    $('.spinner#load').show()
    console.log "2"
    @DownloadUserPromotions renderTemplate
    @

会解决一些奇怪的问题。以前我很确定
您所拥有的行

    @USER = JSON.parse(JSON.stringify(@USER))

永远不会像您预期的那样工作,因为 @USER 将扩展为 this.USER 并且
调用回调时,无论 this 可能是什么,都可能是相当随机的,具体取决于
在您的框架和浏览器上。

First of all you need to get your head the difference between => and -> for defining functions in coffeescript.

=> binds this inside the function to what this was when the function was defined.

-> binds this inside the function to what this was when the function is called

self = this  

is code smell sign in coffeescript that you don't understand how to use the above correctly as you are trying to capture this in order to solve the problem that => solves.

You render function re-written could be

render: ->
    @ReadUserInfo()
    console.log "1"
    renderTemplate = =>
      console.log "5"
      #Below Issue is wierd.......#TODO
      @USER = JSON.parse(JSON.stringify(@USER))
      $(@.el).html clientsPromotionsTemplate
        promos: USER.client_promotions
      $('.spinner#load').hide()
      @.FadeIn()

    $('.spinner#load').show()
    console.log "2"
    @DownloadUserPromotions renderTemplate
    @

And probably will fix some of your wierd issues. Previously I'm pretty sure
that the line you had

    @USER = JSON.parse(JSON.stringify(@USER))

would never have worked as you expected as @USER would be expanded to this.USER and
whatever this might be when the callback is invoked can be pretty random depending
on your framework and browser.

绅士风度i 2024-12-03 14:52:16

由于我的成功回调是 JSON 对象的一部分,因此 Firefox 无法找到它正在寻找的任何默认值,因此不会触发任何内容。获取时指定 dataType:'json' 可以解决此问题,因为 Firefox 知道在哪里查找成功回调。

Chrome 显然读懂了我的心思......

Since my success callback was part of a JSON object, firefox couldn't find whatever default it was looking for and therefore didn't fire anything. Specifying dataType:'json' when fetching solves this problem, because firefox knows where to look for the success call back.

Chrome is apparently reads my mind...

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