Skip to content

Commit 89ae60b

Browse files
committed
backbone 1.0
breaking changes: * parse expects options, not xhr, as second arg * no more Backbone.wrapError * Collection::fetch doesn't call reset by default anymore (though for compatibility, PaginatedCollection still does it on the first fetch) * url is an optionProperty test plan: 1. canvas should work :) Change-Id: Ic68f8c076316efe17474ce25c4191ae0ad01535e Reviewed-on: https://gerrit.instructure.com/22679 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Braden Anderson <banderson@instructure.com> Reviewed-by: Landon Wilkins <lwilkins@instructure.com> Product-Review: Marc LeGendre <marc@instructure.com> QA-Review: Marc LeGendre <marc@instructure.com>
1 parent a040b94 commit 89ae60b

16 files changed

Lines changed: 966 additions & 848 deletions

app/coffeescripts/backbone-ext/Collection.coffee

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ define [
176176
# author:
177177
# foreignKey: 'person_id'
178178
#
179-
parse: (response, xhr) ->
180-
return super unless response.meta?
179+
parse: (response, options) ->
180+
return super unless response?.meta?
181181

182182
primaryCollection = response[response.meta.primaryCollection]
183183
_.each (@sideLoad || {}), (meta, relation) ->
@@ -200,4 +200,4 @@ define [
200200
item[relation] = related
201201
delete item[foreignKey]
202202

203-
super primaryCollection, xhr
203+
super primaryCollection, options

app/coffeescripts/bundles/page_views.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ require [
2828
pageViews = new PageViewCollection
2929
pageViews.url = "/api/v1/users/#{userId}/page_views"
3030

31-
fetchOptions = add: false
31+
fetchOptions = reset: true
3232

3333
pageViewsView = new PageViewView
3434
collection: pageViews

app/coffeescripts/calendar/CalendarEvent.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ define [
4040
success = options.success
4141
delete options.success
4242

43-
error = Backbone.wrapError(options.error, model, options)
43+
error = options.error ? ->
4444
delete options.error
4545

4646
if @get('id')
@@ -52,7 +52,7 @@ define [
5252
[syncResp, syncStatus, syncXhr] = syncArgs
5353
[sectionsResp] = sectionArgs
5454
calEventData = CalendarEvent.mergeSectionsIntoCalendarEvent(syncResp, _.sortBy(sectionsResp, 'id'))
55-
return false unless model.set(model.parse(calEventData, syncXhr), options)
55+
return false unless model.set(model.parse(calEventData), options)
5656
success?(model, calEventData)
5757

5858
$.when(syncDfd, sectionsDfd)

app/coffeescripts/collections/FilesCollection.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ define [
2828
options.data = _.extend content_types: @parentFolder.contentTypes, options.data || {}
2929
super options
3030

31-
parse: (response, xhr) ->
31+
parse: (response) ->
3232
if response
3333
previewUrl = @parentFolder.previewUrl()
3434
_.each response, (file) ->
3535
file.preview_url = if previewUrl
3636
previewUrl.replace('{{id}}', file.id.toString())
3737
else
3838
file.url
39-
super response, xhr
39+
super

app/coffeescripts/collections/FoldersCollection.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ define [
2828

2929
model: Folder
3030

31-
parse: (response, xhr) ->
31+
parse: (response) ->
3232
if response
3333
_.each response, (folder) =>
3434
folder.contentTypes = @parentFolder.contentTypes
35-
super response, xhr
35+
super

app/coffeescripts/collections/OutcomeGroupCollection.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ define [
2525
class OutcomeGroupCollection extends PaginatedCollection
2626
model: OutcomeGroup
2727

28-
parse: (response, xhr) ->
29-
super response, xhr
28+
parse: (response) ->
29+
super
3030
_.reject response, (groupObj) ->
3131
groupObj.id is ENV.COMMON_CORE_GROUP_ID

app/coffeescripts/collections/PaginatedCollection.coffee

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,30 @@ define [
3636

3737
perPageRegex: /\per_page=(\d+)/
3838

39-
##
40-
# have to do this stuff here or else 'reset' and other events are fired
41-
# before _setStateAfterFetch has happened, so the state is just barely off
42-
parse: (response, xhr) ->
43-
@_urlCache ?= []
44-
@_lastFetchOptions ?= {}
45-
@_setStateAfterFetch xhr, @_lastFetchOptions
46-
@_urlCache.push @_lastFetchOptions.url unless @_lastFetchOptions.url in @_urlCache
47-
delete @_lastFetchOptions
48-
super
49-
5039
##
5140
# options.page: 'next', 'prev', 'first', 'last', 'top', 'bottom'
5241
fetch: (options = {}) ->
5342
exclusionFlag = "fetching#{capitalize options.page}Page"
5443
@[exclusionFlag] = true
5544
if options.page?
5645
options.url = @urls[options.page] if @urls?
57-
options.add = true unless options.add?
46+
options.remove = false unless options.remove?
5847
# API keeps params intact, kill data here to avoid appending in super
5948
options.data = ''
60-
@_lastFetchOptions = options
49+
else
50+
# we want the first fetch to reset (since a lot of existing code wants a reset event)
51+
options.reset = true unless options.reset?
6152
@trigger 'beforeFetch', this, options
6253
@trigger "beforeFetch:#{options.page}", this, options if options.page?
63-
super(options).done (response, text, xhr) =>
54+
55+
# have to do this stuff here or else 'reset' and other events are fired
56+
# before _setStateAfterFetch has happened, so the state is just barely off
57+
xhr = null
58+
options.dataFilter = (data) =>
59+
@_setStateAfterFetch(xhr, options)
60+
data
61+
62+
xhr = super(options).done (response, text, xhr) =>
6463
@[exclusionFlag] = false
6564
@trigger 'fetch', this, response, options
6665
@trigger "fetch:#{options.page}", this, response, options if options.page?
@@ -72,8 +71,10 @@ define [
7271
canFetch: (page) ->
7372
@urls? and @urls[page]?
7473

75-
_setStateAfterFetch: (xhr, options={}) =>
74+
_setStateAfterFetch: (xhr, options) =>
75+
@_urlCache ?= []
7676
urlIsNotCached = options.url not in @_urlCache
77+
@_urlCache.push options.url if not urlIsNotCached
7778
firstRequest = !@urls?
7879
setBottom = firstRequest or (options.page in ['next', 'bottom'] and urlIsNotCached)
7980
setTop = firstRequest or (options.page in ['prev', 'top'] and urlIsNotCached)

app/coffeescripts/collections/SyllabusAppointmentGroupsCollection.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ define [
2929

3030
fetch: (options) ->
3131
options ?= {}
32-
options['add'] ?= true
32+
options.remove ?= false
3333

3434
options['data'] ?= {}
3535
options['data']['scope'] = @scope

app/coffeescripts/collections/SyllabusCalendarEventsCollection.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ define [
2929

3030
fetch: (options) ->
3131
options ?= {}
32-
options['add'] ?= true
32+
options.remove ?= false
3333

3434
options['data'] ?= {}
3535
options['data']['type'] = @type

app/coffeescripts/models/Outcome.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ define [
3232
!@has('description')
3333

3434
# overriding to work with both outcome and outcome link responses
35-
parse: (resp, xhr) ->
35+
parse: (resp) ->
3636
if resp.outcome # it's an outcome link
3737
@outcomeLink = resp
3838
@outcomeGroup = resp.outcome_group

0 commit comments

Comments
 (0)