-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add deferred request to models or collections for race condutions #1567
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,6 +215,12 @@ | |
// began. | ||
_pending: null, | ||
|
||
// Deferred request is a request that is waiting for | ||
// response of currently running model request. | ||
// Initial value is false which means | ||
// there is not an currently running request. | ||
_deferredRequest: false, | ||
|
||
// The default name for the JSON `id` attribute is `"id"`. MongoDB and | ||
// CouchDB users may want to set this to `"_id"`. | ||
idAttribute: 'id', | ||
|
@@ -571,6 +577,12 @@ | |
// This should be overridden in most cases. | ||
model: Model, | ||
|
||
// Deferred request is a request that is waiting for | ||
// response of currently running collection request. | ||
// Initial value is false which means | ||
// there is not an currently running request. | ||
_deferredRequest: false, | ||
|
||
// Initialize is an empty function by default. Override it with your own | ||
// initialization logic. | ||
initialize: function(){}, | ||
|
@@ -1387,6 +1399,33 @@ | |
params.processData = false; | ||
} | ||
|
||
var success = options.success; | ||
options.success = function(){ | ||
var dr = model._deferredRequest; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pseudo-code for incoming commits if (!_.isEmpty(dr) && arguments[0].id && model.id === model.cid) {
dr[1].set({id:arguments[0].id}, {silent:true})
} |
||
// running request responsed, | ||
// there is no need to defer another request now | ||
model._deferredRequest = false; | ||
|
||
// if there is a request that deferred until response for this model | ||
if (!_.isEmpty(dr)) { | ||
model.sync(dr[0], dr[1], dr[2]); | ||
} | ||
if (success) success.apply(null, arguments); | ||
}; | ||
|
||
// if there is a running request | ||
if (_.isArray(model._deferredRequest)) { | ||
// defer this request until response of running request. | ||
// if there is another deferred request, | ||
//override previous deferred request | ||
model._deferredRequest = [method, model, options]; | ||
// FIXME: return sth look like xhr | ||
return true; | ||
} | ||
|
||
// last requests until response will defer | ||
// but now model._deferredRequest is empty array | ||
model._deferredRequest = []; | ||
// Make the request, allowing the user to override any Ajax options. | ||
return Backbone.ajax(_.extend(params, options)); | ||
}; | ||
|
@@ -1400,6 +1439,7 @@ | |
Backbone.wrapError = function(onError, originalModel, options) { | ||
return function(model, resp) { | ||
resp = model === originalModel ? resp : model; | ||
resp._deferredRequest = false; | ||
if (onError) { | ||
onError(originalModel, resp, options); | ||
} else { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is pseudo-code for incoming commits