我有一个骨干系列。我习惯fetch
在初始化时将 Facebook 帖子延迟加载到模型中。
https://gist.github.com/2271437
exports.Collection = class Posts extends Backbone.Collection
initialize: (models, options) =>
@id = options.id
@activeDetails = false
@on "loadDetails", @loadDetails
@on "detailsLoaded", @logger
debug "initialized posts"
@fetch
beforeSend: () =>
console.log "about to fetch..."
@trigger "postsLoading"
success: (collection, response) =>
debug "successfully loaded ajax"
@trigger "postsLoaded"
error: (collection, response) => @trigger "postsLoadingError"
出于某种奇怪的原因,当我尝试使用beforeSend
处理程序触发事件时,事件不会触发。我可以调用任何函数,但如果任何函数尝试使用@trigger "eventName"
,则事件永远不会以我能够观察到的方式触发。在上面的示例中,console.log 函数工作得很好,但是触发器失败了。
有任何想法吗?成功和错误处理程序工作出色。