model.fetch 成功回调不会在 Firefox 上触发,但可以在 chrome 上运行
我有一个模型获取的成功回调,并且在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您需要弄清楚 => 之间的区别和->用于在 CoffeeScript 中定义函数。
=>将函数内的 this 绑定到函数定义时的this。
->将函数内的 this 绑定到函数被调用时的 this
是咖啡脚本中您不明白如何使用的代码气味标志当您尝试捕获this以解决 => 的问题时,请正确执行上述操作解决。
您重写的渲染函数可能
会解决一些奇怪的问题。以前我很确定
您所拥有的行
永远不会像您预期的那样工作,因为 @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
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
And probably will fix some of your wierd issues. Previously I'm pretty sure
that the line you had
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.
由于我的成功回调是 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...