Skip to content

Instantly share code, notes, and snippets.

@JonMidhir
Created April 11, 2014 13:15
Show Gist options
  • Save JonMidhir/10467885 to your computer and use it in GitHub Desktop.
Save JonMidhir/10467885 to your computer and use it in GitHub Desktop.
Aborting a Backbone Collection fetch request
# Collection.fetch() returns a Xhr that you can store in a variable.
# The request can be aborted if the Xhr readystate is 1,2 or 3.
#
# Note: If readystate == 3 the request has already been sent and the
# server will continue to process it, the response will be ignored.
class App.Routers.Posts extends Backbone.Router
routes:
'': 'index'
':id': 'show'
initialize: ->
@collection = new App.Collections.Posts()
@view = new App.Views.PostsIndex(collection: @collection, el: '#posts')
index: ->
# Money
@fetchXhr.abort() if @fetchXhr?.readyState in [1..3]
@fetchXhr = @collection.fetch
reset: true
show: (id) ->
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment